@wordpress/api-fetch 7.41.0 → 7.41.1-next.v.202603102151.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -31,9 +31,9 @@ var httpV1Middleware = (options, next) => {
31
31
  options = {
32
32
  ...options,
33
33
  headers: {
34
+ "Content-Type": "application/json",
34
35
  ...options.headers,
35
- "X-HTTP-Method-Override": method,
36
- "Content-Type": "application/json"
36
+ "X-HTTP-Method-Override": method
37
37
  },
38
38
  method: "POST"
39
39
  };
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../src/middlewares/http-v1.ts"],
4
- "sourcesContent": ["/**\n * Internal dependencies\n */\nimport type { APIFetchMiddleware } from '../types';\n\n/**\n * Set of HTTP methods which are eligible to be overridden.\n */\nconst OVERRIDE_METHODS = new Set( [ 'PATCH', 'PUT', 'DELETE' ] );\n\n/**\n * Default request method.\n *\n * \"A request has an associated method (a method). Unless stated otherwise it\n * is `GET`.\"\n *\n * @see https://fetch.spec.whatwg.org/#requests\n */\nconst DEFAULT_METHOD = 'GET';\n\n/**\n * API Fetch middleware which overrides the request method for HTTP v1\n * compatibility leveraging the REST API X-HTTP-Method-Override header.\n *\n * @param options\n * @param next\n */\nconst httpV1Middleware: APIFetchMiddleware = ( options, next ) => {\n\tconst { method = DEFAULT_METHOD } = options;\n\tif ( OVERRIDE_METHODS.has( method.toUpperCase() ) ) {\n\t\toptions = {\n\t\t\t...options,\n\t\t\theaders: {\n\t\t\t\t...options.headers,\n\t\t\t\t'X-HTTP-Method-Override': method,\n\t\t\t\t'Content-Type': 'application/json',\n\t\t\t},\n\t\t\tmethod: 'POST',\n\t\t};\n\t}\n\n\treturn next( options );\n};\n\nexport default httpV1Middleware;\n"],
5
- "mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAQA,IAAM,mBAAmB,oBAAI,IAAK,CAAE,SAAS,OAAO,QAAS,CAAE;AAU/D,IAAM,iBAAiB;AASvB,IAAM,mBAAuC,CAAE,SAAS,SAAU;AACjE,QAAM,EAAE,SAAS,eAAe,IAAI;AACpC,MAAK,iBAAiB,IAAK,OAAO,YAAY,CAAE,GAAI;AACnD,cAAU;AAAA,MACT,GAAG;AAAA,MACH,SAAS;AAAA,QACR,GAAG,QAAQ;AAAA,QACX,0BAA0B;AAAA,QAC1B,gBAAgB;AAAA,MACjB;AAAA,MACA,QAAQ;AAAA,IACT;AAAA,EACD;AAEA,SAAO,KAAM,OAAQ;AACtB;AAEA,IAAO,kBAAQ;",
4
+ "sourcesContent": ["/**\n * Internal dependencies\n */\nimport type { APIFetchMiddleware } from '../types';\n\n/**\n * Set of HTTP methods which are eligible to be overridden.\n */\nconst OVERRIDE_METHODS = new Set( [ 'PATCH', 'PUT', 'DELETE' ] );\n\n/**\n * Default request method.\n *\n * \"A request has an associated method (a method). Unless stated otherwise it\n * is `GET`.\"\n *\n * @see https://fetch.spec.whatwg.org/#requests\n */\nconst DEFAULT_METHOD = 'GET';\n\n/**\n * API Fetch middleware which overrides the request method for HTTP v1\n * compatibility leveraging the REST API X-HTTP-Method-Override header.\n *\n * @param options\n * @param next\n */\nconst httpV1Middleware: APIFetchMiddleware = ( options, next ) => {\n\tconst { method = DEFAULT_METHOD } = options;\n\tif ( OVERRIDE_METHODS.has( method.toUpperCase() ) ) {\n\t\toptions = {\n\t\t\t...options,\n\t\t\theaders: {\n\t\t\t\t'Content-Type': 'application/json',\n\t\t\t\t...options.headers,\n\t\t\t\t'X-HTTP-Method-Override': method,\n\t\t\t},\n\t\t\tmethod: 'POST',\n\t\t};\n\t}\n\n\treturn next( options );\n};\n\nexport default httpV1Middleware;\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAQA,IAAM,mBAAmB,oBAAI,IAAK,CAAE,SAAS,OAAO,QAAS,CAAE;AAU/D,IAAM,iBAAiB;AASvB,IAAM,mBAAuC,CAAE,SAAS,SAAU;AACjE,QAAM,EAAE,SAAS,eAAe,IAAI;AACpC,MAAK,iBAAiB,IAAK,OAAO,YAAY,CAAE,GAAI;AACnD,cAAU;AAAA,MACT,GAAG;AAAA,MACH,SAAS;AAAA,QACR,gBAAgB;AAAA,QAChB,GAAG,QAAQ;AAAA,QACX,0BAA0B;AAAA,MAC3B;AAAA,MACA,QAAQ;AAAA,IACT;AAAA,EACD;AAEA,SAAO,KAAM,OAAQ;AACtB;AAEA,IAAO,kBAAQ;",
6
6
  "names": []
7
7
  }
@@ -7,9 +7,9 @@ var httpV1Middleware = (options, next) => {
7
7
  options = {
8
8
  ...options,
9
9
  headers: {
10
+ "Content-Type": "application/json",
10
11
  ...options.headers,
11
- "X-HTTP-Method-Override": method,
12
- "Content-Type": "application/json"
12
+ "X-HTTP-Method-Override": method
13
13
  },
14
14
  method: "POST"
15
15
  };
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../src/middlewares/http-v1.ts"],
4
- "sourcesContent": ["/**\n * Internal dependencies\n */\nimport type { APIFetchMiddleware } from '../types';\n\n/**\n * Set of HTTP methods which are eligible to be overridden.\n */\nconst OVERRIDE_METHODS = new Set( [ 'PATCH', 'PUT', 'DELETE' ] );\n\n/**\n * Default request method.\n *\n * \"A request has an associated method (a method). Unless stated otherwise it\n * is `GET`.\"\n *\n * @see https://fetch.spec.whatwg.org/#requests\n */\nconst DEFAULT_METHOD = 'GET';\n\n/**\n * API Fetch middleware which overrides the request method for HTTP v1\n * compatibility leveraging the REST API X-HTTP-Method-Override header.\n *\n * @param options\n * @param next\n */\nconst httpV1Middleware: APIFetchMiddleware = ( options, next ) => {\n\tconst { method = DEFAULT_METHOD } = options;\n\tif ( OVERRIDE_METHODS.has( method.toUpperCase() ) ) {\n\t\toptions = {\n\t\t\t...options,\n\t\t\theaders: {\n\t\t\t\t...options.headers,\n\t\t\t\t'X-HTTP-Method-Override': method,\n\t\t\t\t'Content-Type': 'application/json',\n\t\t\t},\n\t\t\tmethod: 'POST',\n\t\t};\n\t}\n\n\treturn next( options );\n};\n\nexport default httpV1Middleware;\n"],
5
- "mappings": ";AAQA,IAAM,mBAAmB,oBAAI,IAAK,CAAE,SAAS,OAAO,QAAS,CAAE;AAU/D,IAAM,iBAAiB;AASvB,IAAM,mBAAuC,CAAE,SAAS,SAAU;AACjE,QAAM,EAAE,SAAS,eAAe,IAAI;AACpC,MAAK,iBAAiB,IAAK,OAAO,YAAY,CAAE,GAAI;AACnD,cAAU;AAAA,MACT,GAAG;AAAA,MACH,SAAS;AAAA,QACR,GAAG,QAAQ;AAAA,QACX,0BAA0B;AAAA,QAC1B,gBAAgB;AAAA,MACjB;AAAA,MACA,QAAQ;AAAA,IACT;AAAA,EACD;AAEA,SAAO,KAAM,OAAQ;AACtB;AAEA,IAAO,kBAAQ;",
4
+ "sourcesContent": ["/**\n * Internal dependencies\n */\nimport type { APIFetchMiddleware } from '../types';\n\n/**\n * Set of HTTP methods which are eligible to be overridden.\n */\nconst OVERRIDE_METHODS = new Set( [ 'PATCH', 'PUT', 'DELETE' ] );\n\n/**\n * Default request method.\n *\n * \"A request has an associated method (a method). Unless stated otherwise it\n * is `GET`.\"\n *\n * @see https://fetch.spec.whatwg.org/#requests\n */\nconst DEFAULT_METHOD = 'GET';\n\n/**\n * API Fetch middleware which overrides the request method for HTTP v1\n * compatibility leveraging the REST API X-HTTP-Method-Override header.\n *\n * @param options\n * @param next\n */\nconst httpV1Middleware: APIFetchMiddleware = ( options, next ) => {\n\tconst { method = DEFAULT_METHOD } = options;\n\tif ( OVERRIDE_METHODS.has( method.toUpperCase() ) ) {\n\t\toptions = {\n\t\t\t...options,\n\t\t\theaders: {\n\t\t\t\t'Content-Type': 'application/json',\n\t\t\t\t...options.headers,\n\t\t\t\t'X-HTTP-Method-Override': method,\n\t\t\t},\n\t\t\tmethod: 'POST',\n\t\t};\n\t}\n\n\treturn next( options );\n};\n\nexport default httpV1Middleware;\n"],
5
+ "mappings": ";AAQA,IAAM,mBAAmB,oBAAI,IAAK,CAAE,SAAS,OAAO,QAAS,CAAE;AAU/D,IAAM,iBAAiB;AASvB,IAAM,mBAAuC,CAAE,SAAS,SAAU;AACjE,QAAM,EAAE,SAAS,eAAe,IAAI;AACpC,MAAK,iBAAiB,IAAK,OAAO,YAAY,CAAE,GAAI;AACnD,cAAU;AAAA,MACT,GAAG;AAAA,MACH,SAAS;AAAA,QACR,gBAAgB;AAAA,QAChB,GAAG,QAAQ;AAAA,QACX,0BAA0B;AAAA,MAC3B;AAAA,MACA,QAAQ;AAAA,IACT;AAAA,EACD;AAEA,SAAO,KAAM,OAAQ;AACtB;AAEA,IAAO,kBAAQ;",
6
6
  "names": []
7
7
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wordpress/api-fetch",
3
- "version": "7.41.0",
3
+ "version": "7.41.1-next.v.202603102151.0+59e17f9ec",
4
4
  "description": "Utility to make WordPress REST API requests.",
5
5
  "author": "The WordPress Contributors",
6
6
  "license": "GPL-2.0-or-later",
@@ -45,11 +45,11 @@
45
45
  "wpScriptDefaultExport": true,
46
46
  "types": "build-types",
47
47
  "dependencies": {
48
- "@wordpress/i18n": "^6.14.0",
49
- "@wordpress/url": "^4.41.0"
48
+ "@wordpress/i18n": "^6.14.1-next.v.202603102151.0+59e17f9ec",
49
+ "@wordpress/url": "^4.41.1-next.v.202603102151.0+59e17f9ec"
50
50
  },
51
51
  "publishConfig": {
52
52
  "access": "public"
53
53
  },
54
- "gitHead": "8bfc179b9aed74c0a6dd6e8edf7a49e40e4f87cc"
54
+ "gitHead": "86db21e727d89e8f0dbba9300d2f97fd22b08693"
55
55
  }
@@ -31,9 +31,9 @@ const httpV1Middleware: APIFetchMiddleware = ( options, next ) => {
31
31
  options = {
32
32
  ...options,
33
33
  headers: {
34
+ 'Content-Type': 'application/json',
34
35
  ...options.headers,
35
36
  'X-HTTP-Method-Override': method,
36
- 'Content-Type': 'application/json',
37
37
  },
38
38
  method: 'POST',
39
39
  };
@@ -39,4 +39,213 @@ describe( 'HTTP v1 Middleware', () => {
39
39
 
40
40
  httpV1Middleware( requestOptions, callback );
41
41
  } );
42
+
43
+ it( 'should default Content-Type to application/json when no headers are set', () => {
44
+ expect.hasAssertions();
45
+
46
+ const callback: FetchHandler = async ( options ) => {
47
+ expect( options.headers![ 'Content-Type' ] ).toBe(
48
+ 'application/json'
49
+ );
50
+ };
51
+
52
+ httpV1Middleware( { method: 'PUT', data: {} }, callback );
53
+ } );
54
+
55
+ it( 'should default Content-Type to application/json when headers exist without Content-Type', () => {
56
+ expect.hasAssertions();
57
+
58
+ const callback: FetchHandler = async ( options ) => {
59
+ expect( options.headers![ 'Content-Type' ] ).toBe(
60
+ 'application/json'
61
+ );
62
+ expect( options.headers![ 'X-Custom' ] ).toBe( 'value' );
63
+ };
64
+
65
+ httpV1Middleware(
66
+ {
67
+ method: 'PUT',
68
+ headers: { 'X-Custom': 'value' },
69
+ data: {},
70
+ },
71
+ callback
72
+ );
73
+ } );
74
+
75
+ it( 'should respect a caller-provided Content-Type header', () => {
76
+ expect.hasAssertions();
77
+
78
+ const callback: FetchHandler = async ( options ) => {
79
+ expect( options.headers![ 'Content-Type' ] ).toBe( 'text/plain' );
80
+ expect( options.method ).toBe( 'POST' );
81
+ expect( options.headers![ 'X-HTTP-Method-Override' ] ).toBe(
82
+ 'PUT'
83
+ );
84
+ };
85
+
86
+ httpV1Middleware(
87
+ {
88
+ method: 'PUT',
89
+ headers: { 'Content-Type': 'text/plain' },
90
+ body: '# Markdown content',
91
+ },
92
+ callback
93
+ );
94
+ } );
95
+
96
+ it( 'should respect Content-Type for PATCH requests', () => {
97
+ expect.hasAssertions();
98
+
99
+ const callback: FetchHandler = async ( options ) => {
100
+ expect( options.headers![ 'Content-Type' ] ).toBe(
101
+ 'application/xml'
102
+ );
103
+ expect( options.headers![ 'X-HTTP-Method-Override' ] ).toBe(
104
+ 'PATCH'
105
+ );
106
+ };
107
+
108
+ httpV1Middleware(
109
+ {
110
+ method: 'PATCH',
111
+ headers: { 'Content-Type': 'application/xml' },
112
+ body: '<doc/>',
113
+ },
114
+ callback
115
+ );
116
+ } );
117
+
118
+ it( 'should respect Content-Type for DELETE requests', () => {
119
+ expect.hasAssertions();
120
+
121
+ const callback: FetchHandler = async ( options ) => {
122
+ expect( options.headers![ 'Content-Type' ] ).toBe( 'text/plain' );
123
+ expect( options.headers![ 'X-HTTP-Method-Override' ] ).toBe(
124
+ 'DELETE'
125
+ );
126
+ };
127
+
128
+ httpV1Middleware(
129
+ {
130
+ method: 'DELETE',
131
+ headers: { 'Content-Type': 'text/plain' },
132
+ },
133
+ callback
134
+ );
135
+ } );
136
+
137
+ it( 'should always set X-HTTP-Method-Override regardless of caller headers', () => {
138
+ expect.hasAssertions();
139
+
140
+ const callback: FetchHandler = async ( options ) => {
141
+ // X-HTTP-Method-Override must not be overridable by the caller.
142
+ expect( options.headers![ 'X-HTTP-Method-Override' ] ).toBe(
143
+ 'PUT'
144
+ );
145
+ };
146
+
147
+ httpV1Middleware(
148
+ {
149
+ method: 'PUT',
150
+ headers: { 'X-HTTP-Method-Override': 'DELETE' },
151
+ data: {},
152
+ },
153
+ callback
154
+ );
155
+ } );
156
+
157
+ it( 'should preserve other caller headers alongside Content-Type override', () => {
158
+ expect.hasAssertions();
159
+
160
+ const callback: FetchHandler = async ( options ) => {
161
+ expect( options.headers![ 'Content-Type' ] ).toBe(
162
+ 'text/markdown'
163
+ );
164
+ expect( options.headers!.Authorization ).toBe( 'Bearer token123' );
165
+ expect( options.headers![ 'X-Custom-Header' ] ).toBe(
166
+ 'custom-value'
167
+ );
168
+ expect( options.headers![ 'X-HTTP-Method-Override' ] ).toBe(
169
+ 'PUT'
170
+ );
171
+ };
172
+
173
+ httpV1Middleware(
174
+ {
175
+ method: 'PUT',
176
+ headers: {
177
+ 'Content-Type': 'text/markdown',
178
+ Authorization: 'Bearer token123',
179
+ 'X-Custom-Header': 'custom-value',
180
+ },
181
+ body: '# Hello',
182
+ },
183
+ callback
184
+ );
185
+ } );
186
+
187
+ it( 'should not modify GET requests even with custom Content-Type', () => {
188
+ expect.hasAssertions();
189
+
190
+ const requestOptions = {
191
+ method: 'GET' as const,
192
+ path: '/wp/v2/posts',
193
+ headers: { 'Content-Type': 'text/plain' },
194
+ };
195
+
196
+ const callback: FetchHandler = async ( options ) => {
197
+ expect( options ).toBe( requestOptions );
198
+ };
199
+
200
+ httpV1Middleware( requestOptions, callback );
201
+ } );
202
+
203
+ it( 'should not modify POST requests', () => {
204
+ expect.hasAssertions();
205
+
206
+ const requestOptions = {
207
+ method: 'POST' as const,
208
+ path: '/wp/v2/posts',
209
+ headers: { 'Content-Type': 'text/plain' },
210
+ body: 'raw content',
211
+ };
212
+
213
+ const callback: FetchHandler = async ( options ) => {
214
+ expect( options ).toBe( requestOptions );
215
+ };
216
+
217
+ httpV1Middleware( requestOptions, callback );
218
+ } );
219
+
220
+ it( 'should handle case-insensitive method matching', () => {
221
+ expect.hasAssertions();
222
+
223
+ const callback: FetchHandler = async ( options ) => {
224
+ expect( options.method ).toBe( 'POST' );
225
+ expect( options.headers![ 'X-HTTP-Method-Override' ] ).toBe(
226
+ 'put'
227
+ );
228
+ };
229
+
230
+ httpV1Middleware( { method: 'put', data: {} }, callback );
231
+ } );
232
+
233
+ it( 'should preserve the body when overriding Content-Type', () => {
234
+ expect.hasAssertions();
235
+
236
+ const markdownContent = '# Title\n\nSome **bold** text.\n';
237
+ const callback: FetchHandler = async ( options ) => {
238
+ expect( options.body ).toBe( markdownContent );
239
+ expect( options.headers![ 'Content-Type' ] ).toBe( 'text/plain' );
240
+ };
241
+
242
+ httpV1Middleware(
243
+ {
244
+ method: 'PUT',
245
+ headers: { 'Content-Type': 'text/plain' },
246
+ body: markdownContent,
247
+ },
248
+ callback
249
+ );
250
+ } );
42
251
  } );