Skip to content

Commit a07b22a

Browse files
committedJan 20, 2022
fix(services): normalised fetch methods
`patch` could failed with lowercase method name
1 parent ad267ca commit a07b22a

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed
 

‎src/Calliope/Concerns/CallsApi.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ import { isObjectLiteral } from '../../Support/function';
1111
import { finish, kebab, plural } from '../../Support/string';
1212
import type { MaybeArray } from '../../Support/type';
1313

14-
export type Method = 'delete' | 'get' | 'patch' | 'post' | 'put';
14+
/**
15+
* The request methods.
16+
*/
17+
export type Method = 'DELETE' | 'delete' | 'GET' | 'get' | 'PATCH' | 'patch' | 'POST' | 'post' | 'PUT' | 'put';
1518

1619
export default class CallsApi extends BuildsQuery {
1720
/**

‎src/Services/API.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export default class API implements ApiCaller {
5959
* Prepare/compile the ajax call initialisation.
6060
*
6161
* @param {string} url - The endpoint the request goes to.
62-
* @param {'get' | 'post' | 'delete' | 'patch' | 'put'} method - The method the request uses.
62+
* @param {Method} method - The method the request uses.
6363
* @param {object=} data - The optional data to send with the request.
6464
* @param {object=} customHeaders - Custom headers to merge into the request.
6565
* @param {object=} queryParameters - The query parameters to append to the url
@@ -75,7 +75,8 @@ export default class API implements ApiCaller {
7575
customHeaders?: Record<string, MaybeArray<string>>,
7676
queryParameters?: Record<string, unknown>
7777
): Promise<{ url: string; requestInit: RequestInit }> {
78-
const initOptions: RequestInit = { method: method.toLowerCase() };
78+
// normalising fetch methods https://fetch.spec.whatwg.org/#concept-method-normalize
79+
const initOptions: RequestInit = { method: method.toUpperCase() };
7980
const configHeaders = new Headers(new GlobalConfig().get('headers'));
8081
queryParameters = queryParameters ?? {};
8182

@@ -100,17 +101,17 @@ export default class API implements ApiCaller {
100101

101102
// ensure method is explicitly set if previously
102103
// removed by initRequest or requestOptions
103-
initOptions.method = initOptions.method ?? 'get';
104+
initOptions.method = initOptions.method ?? 'GET';
104105

105-
if (initOptions.method === 'get') {
106+
if (initOptions.method === 'GET') {
106107
// given if there was any body it was merged in above,
107108
// we delete it as GET cannot have a body
108109
delete initOptions.body;
109110
}
110111

111112
if (isObjectLiteral(data) && Object.keys(data).length || data instanceof FormData) {
112113
// if not a GET method
113-
if (initOptions.method && initOptions.method !== 'get') {
114+
if (initOptions.method && initOptions.method !== 'GET') {
114115
if (data instanceof FormData) {
115116
if (!headers.has('Content-Type')) {
116117
headers.set('Content-Type', 'multipart/form-data');

0 commit comments

Comments
 (0)