Skip to content

Commit 6565ed0

Browse files
committed
- Added with credentials
- Fixed local development settings in tsconfig
1 parent d95de6e commit 6565ed0

File tree

5 files changed

+114
-111
lines changed

5 files changed

+114
-111
lines changed

src/templates/core/OpenAPI.hbs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ interface Config {
77
BASE: string;
88
VERSION: string;
99
CLIENT: 'fetch' | 'xhr';
10+
WITH_CREDENTIALS: boolean;
1011
TOKEN: string;
1112
}
1213

1314
export const OpenAPI: Config = {
1415
BASE: '{{{server}}}',
1516
VERSION: '{{{version}}}',
1617
CLIENT: '{{{httpClient}}}',
18+
WITH_CREDENTIALS: false,
1719
TOKEN: '',
1820
};

src/templates/core/request.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,15 @@ export async function request(options: Readonly<RequestOptions>): Promise<Result
3232
const request: RequestInit = {
3333
headers,
3434
method: options.method,
35-
credentials: 'same-origin',
3635
};
3736

37+
// If we specified to send requests with credentials, then we
38+
// set the request credentials options to include. This is only
39+
// needed if you make cross-origin calls.
40+
if (OpenAPI.WITH_CREDENTIALS) {
41+
request.credentials = 'include';
42+
}
43+
3844
// If we have a bearer token then we set the authentication header.
3945
if (OpenAPI.TOKEN !== null && OpenAPI.TOKEN !== '') {
4046
headers.append('Authorization', `Bearer ${OpenAPI.TOKEN}`);

src/templates/core/requestUsingXHR.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ export async function requestUsingXHR(url: string, request: Readonly<RequestInit
6363
// because the request needs to be initialized!
6464
xhr.open(request.method!, url, true);
6565

66+
// When request credentials are set to include then this is
67+
// the same behaviour as withCredentials = true in XHR:
68+
// https://developer.mozilla.org/en-US/docs/Web/API/Request/credentials
69+
xhr.withCredentials = request.credentials === 'include';
70+
6671
// Add the headers (required when dealing with JSON)
6772
const headers = request.headers as Headers;
6873
headers.forEach((value: string, key: string): void => {

tsconfig.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
"outDir": "./dist",
66
"target": "ES2017",
77
"module": "ES6",
8-
"moduleResolution": "node",
9-
"lib": ["ES2017"],
10-
"types": ["node", "jest"],
8+
"moduleResolution": "Node",
9+
"lib": ["ES2017", "DOM"],
10+
"types": ["jest", "node"],
1111
"typeRoots": ["node_modules/@types"],
1212
"declaration": false,
1313
"declarationMap": false,

0 commit comments

Comments
 (0)