Skip to content

Commit c9b232e

Browse files
committed
Merge branch 'feature/with-credentials'
2 parents f31044b + 53c6f6d commit c9b232e

File tree

6 files changed

+156
-124
lines changed

6 files changed

+156
-124
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 => {

test/__snapshots__/index.spec.js.snap

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,15 @@ interface Config {
7171
BASE: string;
7272
VERSION: string;
7373
CLIENT: 'fetch' | 'xhr';
74+
WITH_CREDENTIALS: boolean;
7475
TOKEN: string;
7576
}
7677

7778
export const OpenAPI: Config = {
7879
BASE: 'http://localhost:8080/api',
7980
VERSION: '9.0',
8081
CLIENT: 'fetch',
82+
WITH_CREDENTIALS: false,
8183
TOKEN: '',
8284
};"
8385
`;
@@ -229,9 +231,15 @@ export async function request(options: Readonly<RequestOptions>): Promise<Result
229231
const request: RequestInit = {
230232
headers,
231233
method: options.method,
232-
credentials: 'same-origin',
233234
};
234235

236+
// If we specified to send requests with credentials, then we
237+
// set the request credentials options to include. This is only
238+
// needed if you make cross-origin calls.
239+
if (OpenAPI.WITH_CREDENTIALS) {
240+
request.credentials = 'include';
241+
}
242+
235243
// If we have a bearer token then we set the authentication header.
236244
if (OpenAPI.TOKEN !== null && OpenAPI.TOKEN !== '') {
237245
headers.append('Authorization', \`Bearer \${OpenAPI.TOKEN}\`);
@@ -423,6 +431,11 @@ export async function requestUsingXHR(url: string, request: Readonly<RequestInit
423431
// because the request needs to be initialized!
424432
xhr.open(request.method!, url, true);
425433

434+
// When request credentials are set to include then this is
435+
// the same behaviour as withCredentials = true in XHR:
436+
// https://developer.mozilla.org/en-US/docs/Web/API/Request/credentials
437+
xhr.withCredentials = request.credentials === 'include';
438+
426439
// Add the headers (required when dealing with JSON)
427440
const headers = request.headers as Headers;
428441
headers.forEach((value: string, key: string): void => {
@@ -2706,13 +2719,15 @@ interface Config {
27062719
BASE: string;
27072720
VERSION: string;
27082721
CLIENT: 'fetch' | 'xhr';
2722+
WITH_CREDENTIALS: boolean;
27092723
TOKEN: string;
27102724
}
27112725

27122726
export const OpenAPI: Config = {
27132727
BASE: '/api',
27142728
VERSION: '1',
27152729
CLIENT: 'fetch',
2730+
WITH_CREDENTIALS: false,
27162731
TOKEN: '',
27172732
};"
27182733
`;
@@ -2864,9 +2879,15 @@ export async function request(options: Readonly<RequestOptions>): Promise<Result
28642879
const request: RequestInit = {
28652880
headers,
28662881
method: options.method,
2867-
credentials: 'same-origin',
28682882
};
28692883

2884+
// If we specified to send requests with credentials, then we
2885+
// set the request credentials options to include. This is only
2886+
// needed if you make cross-origin calls.
2887+
if (OpenAPI.WITH_CREDENTIALS) {
2888+
request.credentials = 'include';
2889+
}
2890+
28702891
// If we have a bearer token then we set the authentication header.
28712892
if (OpenAPI.TOKEN !== null && OpenAPI.TOKEN !== '') {
28722893
headers.append('Authorization', \`Bearer \${OpenAPI.TOKEN}\`);
@@ -3058,6 +3079,11 @@ export async function requestUsingXHR(url: string, request: Readonly<RequestInit
30583079
// because the request needs to be initialized!
30593080
xhr.open(request.method!, url, true);
30603081

3082+
// When request credentials are set to include then this is
3083+
// the same behaviour as withCredentials = true in XHR:
3084+
// https://developer.mozilla.org/en-US/docs/Web/API/Request/credentials
3085+
xhr.withCredentials = request.credentials === 'include';
3086+
30613087
// Add the headers (required when dealing with JSON)
30623088
const headers = request.headers as Headers;
30633089
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)