Comparing version 2.0.8 to 2.0.9
@@ -0,1 +1,7 @@ | ||
## [2.0.9](https://github.com/posva/mande/compare/v2.0.8...v2.0.9) (2024-05-12) | ||
### Reverts | ||
- smaller ([9f09df6](https://github.com/posva/mande/commit/9f09df678bd62aec36bc474535c405444267ec64)) | ||
## [2.0.8](https://github.com/posva/mande/compare/v2.0.7...v2.0.8) (2023-10-03) | ||
@@ -82,3 +88,3 @@ | ||
- **nuxt:** missign cookies ([c9b02d2](https://github.com/posva/mande/commit/c9b02d25ce4511e1611b4f38972038c144c2428e)) | ||
- **nuxt:** missing cookies ([c9b02d2](https://github.com/posva/mande/commit/c9b02d25ce4511e1611b4f38972038c144c2428e)) | ||
@@ -101,3 +107,3 @@ ## [0.0.22](https://github.com/posva/mande/compare/v0.0.21...v0.0.22) (2020-08-09) | ||
- **nuxt:** pass errros to ctx.error ([b71b043](https://github.com/posva/mande/commit/b71b043a526d7d1719d072f4f60e86d632b46082)) | ||
- **nuxt:** pass errors to ctx.error ([b71b043](https://github.com/posva/mande/commit/b71b043a526d7d1719d072f4f60e86d632b46082)) | ||
@@ -104,0 +110,0 @@ ## [0.0.19](https://github.com/posva/mande/compare/v0.0.18...v0.0.19) (2020-07-28) |
@@ -23,3 +23,3 @@ /** | ||
* @param baseURL - absolute url | ||
* @param instanceOptions - optional options that will be applied to every | ||
* @param passedInstanceOptions - optional options that will be applied to every | ||
* other request for this instance | ||
@@ -46,2 +46,12 @@ */ | ||
/** | ||
* Sends a GET request to the base url. This is equivalent to calling get with an empty string. | ||
* | ||
* @example | ||
* ```js | ||
* const userList = await users.get() | ||
* ``` | ||
* @param options - optional {@link Options} | ||
*/ | ||
get<T = unknown, R extends ResponseAsTypes = 'json'>(options?: Options<R>): MandeResponse<T, R>; | ||
/** | ||
* Sends a GET request to the given url. | ||
@@ -51,5 +61,3 @@ * | ||
* ```js | ||
* users.get('2').then(user => { | ||
* // do something | ||
* }) | ||
* const userProfile = await users.get('2') | ||
* ``` | ||
@@ -59,5 +67,15 @@ * @param url - optional relative url to send the request to | ||
*/ | ||
get<T = unknown, R extends ResponseAsTypes = 'json'>(options?: Options<R>): MandeResponse<T, R>; | ||
get<T = unknown, R extends ResponseAsTypes = 'json'>(url: string | number, options?: Options<R>): MandeResponse<T, R>; | ||
/** | ||
* Sends a POST request to the base url. This is equivalent to calling post with an empty string. | ||
* | ||
* @example | ||
* ```js | ||
* const createdUser = await users.post({ name: 'Eduardo' }) | ||
* ``` | ||
* @param data - optional body of the request | ||
* @param options - optional {@link Options} | ||
*/ | ||
post<T = unknown, R extends ResponseAsTypes = 'json'>(data?: any, options?: Options<R>): MandeResponse<T, R>; | ||
/** | ||
* Sends a POST request to the given url. | ||
@@ -67,5 +85,3 @@ * | ||
* ```js | ||
* users.post('', { name: 'Eduardo' }).then(user => { | ||
* // do something | ||
* }) | ||
* const createdUser = await users.post('', { name: 'Eduardo' }) | ||
* ``` | ||
@@ -76,5 +92,15 @@ * @param url - relative url to send the request to | ||
*/ | ||
post<T = unknown, R extends ResponseAsTypes = 'json'>(data?: any, options?: Options<R>): MandeResponse<T, R>; | ||
post<T = unknown, R extends ResponseAsTypes = 'json'>(url: string | number, data?: any, options?: Options<R>): MandeResponse<T, R>; | ||
/** | ||
* Sends a PUT request to the base url. This is equivalent to calling put with an empty string. | ||
* | ||
* @example | ||
* ```js | ||
* users.put({ name: 'Eduardo' }) | ||
* ``` | ||
* @param data - optional body of the request | ||
* @param options - optional {@link Options} | ||
*/ | ||
put<T = unknown, R extends ResponseAsTypes = 'json'>(data?: any, options?: Options<R>): MandeResponse<T, R>; | ||
/** | ||
* Sends a PUT request to the given url. | ||
@@ -84,5 +110,3 @@ * | ||
* ```js | ||
* users.put('2', { name: 'Eduardo' }).then(user => { | ||
* // do something | ||
* }) | ||
* users.put('2', { name: 'Eduardo' }) | ||
* ``` | ||
@@ -93,14 +117,10 @@ * @param url - relative url to send the request to | ||
*/ | ||
put<T = unknown, R extends ResponseAsTypes = 'json'>(data?: any, options?: Options<R>): MandeResponse<T, R>; | ||
put<T = unknown, R extends ResponseAsTypes = 'json'>(url: string | number, data?: any, options?: Options<R>): MandeResponse<T, R>; | ||
/** | ||
* Sends a PATCH request to the given url. | ||
* Sends a PATCH request to the base url. This is equivalent to calling patch with an empty string. | ||
* | ||
* @example | ||
* ```js | ||
* users.patch('2', { name: 'Eduardo' }).then(user => { | ||
* // do something | ||
* }) | ||
* users.patch({ name: 'Eduardo' }) | ||
* ``` | ||
* @param url - relative url to send the request to | ||
* @param data - optional body of the request | ||
@@ -110,4 +130,22 @@ * @param options - optional {@link Options} | ||
patch<T = unknown, R extends ResponseAsTypes = 'json'>(data?: any, options?: Options<R>): MandeResponse<T, R>; | ||
/** | ||
* Sends a PATCH request to the given url. | ||
* | ||
* @example | ||
* ```js | ||
* users.patch('2', { name: 'Eduardo' }) | ||
* ``` | ||
*/ | ||
patch<T = unknown, R extends ResponseAsTypes = 'json'>(url: string | number, data?: any, options?: Options<R>): MandeResponse<T, R>; | ||
/** | ||
* Sends a DELETE request to the base url. This is equivalent to calling delete with an empty string. | ||
* | ||
* @example | ||
* ```js | ||
* users.delete() | ||
* ``` | ||
* @param options - optional {@link Options} | ||
*/ | ||
delete<T = unknown, R extends ResponseAsTypes = 'json'>(options?: Options<R>): MandeResponse<T, R>; | ||
/** | ||
* Sends a DELETE request to the given url. | ||
@@ -117,5 +155,3 @@ * | ||
* ```js | ||
* users.delete('2').then(user => { | ||
* // do something | ||
* }) | ||
* users.delete('2') | ||
* ``` | ||
@@ -125,3 +161,2 @@ * @param url - relative url to send the request to | ||
*/ | ||
delete<T = unknown, R extends ResponseAsTypes = 'json'>(options?: Options<R>): MandeResponse<T, R>; | ||
delete<T = unknown, R extends ResponseAsTypes = 'json'>(url: string | number, options?: Options<R>): MandeResponse<T, R>; | ||
@@ -128,0 +163,0 @@ } |
/*! | ||
* mande v2.0.7 | ||
* (c) 2023 Eduardo San Martin Morote | ||
* mande v2.0.8 | ||
* (c) 2024 Eduardo San Martin Morote | ||
* @license MIT | ||
@@ -45,3 +45,4 @@ */ | ||
Accept: 'application/json', | ||
'Content-Type': 'application/json', | ||
// NOTE: instead of passing json here, we pass it when creating the request to automatically handle FormData | ||
// 'Content-Type': data instanceof FormData ? null : 'application/json', | ||
}, | ||
@@ -61,3 +62,3 @@ stringify: JSON.stringify, | ||
* @param baseURL - absolute url | ||
* @param instanceOptions - optional options that will be applied to every | ||
* @param passedInstanceOptions - optional options that will be applied to every | ||
* other request for this instance | ||
@@ -91,3 +92,5 @@ */ | ||
// we need to ditch nullish headers | ||
headers: removeNullishValues(Object.assign(Object.assign(Object.assign({}, defaults.headers), instanceOptions.headers), localOptions.headers)) }); | ||
headers: removeNullishValues(Object.assign(Object.assign(Object.assign({ | ||
// let the browser automatically set the content-type with FormData | ||
'Content-Type': data instanceof FormData ? null : 'application/json' }, defaults.headers), instanceOptions.headers), localOptions.headers)) }); | ||
let query = Object.assign(Object.assign(Object.assign({}, defaults.query), instanceOptions.query), localOptions.query); | ||
@@ -187,6 +190,4 @@ let { responseAs } = mergedOptions; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
return exports; | ||
})({}); |
/*! | ||
* mande v2.0.7 | ||
* (c) 2023 Eduardo San Martin Morote | ||
* mande v2.0.8 | ||
* (c) 2024 Eduardo San Martin Morote | ||
* @license MIT | ||
*/ | ||
var Mande=function(e){"use strict";let t=/^\/+/;const n={responseAs:"json",headers:{Accept:"application/json","Content-Type":"application/json"},stringify:JSON.stringify};return e.defaults=n,e.mande=function(e,s={},r){function o(s,o,a,c={}){let u,l;"object"==typeof o?(u="",c=a||o||{},l=o):(u=o,l=a);let d=Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({},n),i),{method:s}),c),{headers:(f=Object.assign(Object.assign(Object.assign({},n.headers),i.headers),c.headers),Object.keys(f).reduce(((e,t)=>(null!=f[t]&&(e[t]=f[t]),e)),{}))});var f;let p=Object.assign(Object.assign(Object.assign({},n.query),i.query),c.query),{responseAs:b}=d;u=function(e,n){return e+(n&&(e.endsWith("/")?n.replace(t,""):n.startsWith("/")?n:"/"+n))}(e,"number"==typeof u?""+u:u||""),u+=function(e){let t=Object.keys(e).map((t=>[t,e[t]].map(encodeURIComponent).join("="))).join("&");return t?"?"+t:""}(p),"P"===s[0]&&l&&!d.body&&(d.body=l instanceof FormData?l:d.stringify(l));const j="undefined"!=typeof fetch?fetch:r;if(!j)throw new Error("No fetch function exists. Make sure to include a polyfill on Node.js.");return j(u,d).then((e=>Promise.all([e,"response"===b?e:e[b]().catch((()=>null))]))).then((([e,t])=>{if(e.status>=200&&e.status<300)return"response"!==b&&204==e.status?null:t;let n=new Error(e.statusText);throw n.response=e,n.body=t,n}))}const i=Object.assign({query:{},headers:{}},s);return{options:i,post:o.bind(null,"POST"),put:o.bind(null,"PUT"),patch:o.bind(null,"PATCH"),get:(e,t)=>o("GET",e,null,t),delete:(e,t)=>o("DELETE",e,null,t)}},e.nuxtWrap=function(e,t){const n=t.length;return function(){let s=e,r=Array.from(arguments);if(arguments.length===n){s=Object.assign({},e);const[t]=r.splice(0,1);t(s)}return t.call(null,s,...r)}},Object.defineProperty(e,"__esModule",{value:!0}),e}({}); | ||
var Mande=function(e){"use strict";let t=/^\/+/;const n={responseAs:"json",headers:{Accept:"application/json"},stringify:JSON.stringify};return e.defaults=n,e.mande=function(e,s={},r){function o(s,o,a,c={}){let l,u;"object"==typeof o?(l="",c=a||o||{},u=o):(l=o,u=a);let f=Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({},n),i),{method:s}),c),{headers:(p=Object.assign(Object.assign(Object.assign({"Content-Type":u instanceof FormData?null:"application/json"},n.headers),i.headers),c.headers),Object.keys(p).reduce(((e,t)=>(null!=p[t]&&(e[t]=p[t]),e)),{}))});var p;let d=Object.assign(Object.assign(Object.assign({},n.query),i.query),c.query),{responseAs:b}=f;l=function(e,n){return e+(n&&(e.endsWith("/")?n.replace(t,""):n.startsWith("/")?n:"/"+n))}(e,"number"==typeof l?""+l:l||""),l+=function(e){let t=Object.keys(e).map((t=>[t,e[t]].map(encodeURIComponent).join("="))).join("&");return t?"?"+t:""}(d),"P"===s[0]&&u&&!f.body&&(f.body=u instanceof FormData?u:f.stringify(u));const j="undefined"!=typeof fetch?fetch:r;if(!j)throw new Error("No fetch function exists. Make sure to include a polyfill on Node.js.");return j(l,f).then((e=>Promise.all([e,"response"===b?e:e[b]().catch((()=>null))]))).then((([e,t])=>{if(e.status>=200&&e.status<300)return"response"!==b&&204==e.status?null:t;let n=new Error(e.statusText);throw n.response=e,n.body=t,n}))}const i=Object.assign({query:{},headers:{}},s);return{options:i,post:o.bind(null,"POST"),put:o.bind(null,"PUT"),patch:o.bind(null,"PATCH"),get:(e,t)=>o("GET",e,null,t),delete:(e,t)=>o("DELETE",e,null,t)}},e.nuxtWrap=function(e,t){const n=t.length;return function(){let s=e,r=Array.from(arguments);if(arguments.length===n){s=Object.assign({},e);const[t]=r.splice(0,1);t(s)}return t.call(null,s,...r)}},e}({}); |
{ | ||
"name": "mande", | ||
"version": "2.0.8", | ||
"version": "2.0.9", | ||
"description": "800 bytes modern wrapper around fetch with smart defaults", | ||
"packageManager": "pnpm@8.8.0", | ||
"packageManager": "pnpm@9.1.0", | ||
"type": "commonjs", | ||
"main": "index.js", | ||
@@ -38,13 +39,13 @@ "module": "dist/mande.mjs", | ||
"lint": "prettier -c --parser typescript \"{src,__tests__}/**/*.[jt]s?(x)\"", | ||
"lint:fix": "yarn run lint --write", | ||
"lint:fix": "pnpm run lint --write", | ||
"test:unit": "vitest", | ||
"release": "bash scripts/release.sh", | ||
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s -r 1", | ||
"dev": "yarn run test:unit --watchAll", | ||
"pretest": "yarn run lint", | ||
"test": "yarn run test:unit run && yarn run build", | ||
"dev": "vitest", | ||
"pretest": "pnpm run lint", | ||
"test": "pnpm run test:unit run && pnpm run build", | ||
"size": "size-limit", | ||
"build": "rollup -c rollup.config.js", | ||
"build": "rollup -c rollup.config.mjs", | ||
"build:dts": "api-extractor run --local --verbose", | ||
"docs": "api-documenter markdown -i temp -o docs" | ||
"docs": "typedoc" | ||
}, | ||
@@ -76,3 +77,5 @@ "files": [ | ||
{ | ||
"path": "size-check/index.js" | ||
"name": "Mande", | ||
"path": "dist/mande.prod.mjs", | ||
"import": "{ mande }" | ||
} | ||
@@ -82,22 +85,22 @@ ], | ||
"devDependencies": { | ||
"@microsoft/api-documenter": "^7.23.9", | ||
"@microsoft/api-extractor": "^7.38.0", | ||
"@nuxt/types": "^2.17.1", | ||
"@rollup/plugin-alias": "^4.0.4", | ||
"@rollup/plugin-replace": "^5.0.2", | ||
"@size-limit/preset-small-lib": "^8.2.6", | ||
"@vitest/coverage-v8": "^0.34.6", | ||
"codecov": "^3.8.3", | ||
"conventional-changelog-cli": "^2.2.2", | ||
"pascalcase": "^1.0.0", | ||
"prettier": "^2.8.8", | ||
"@microsoft/api-extractor": "^7.43.1", | ||
"@nuxt/types": "^2.17.3", | ||
"@rollup/plugin-alias": "^5.1.0", | ||
"@rollup/plugin-commonjs": "^25.0.7", | ||
"@rollup/plugin-node-resolve": "^15.2.3", | ||
"@rollup/plugin-replace": "^5.0.5", | ||
"@rollup/plugin-terser": "^0.4.4", | ||
"@size-limit/preset-small-lib": "^11.1.2", | ||
"@vitest/coverage-v8": "^1.6.0", | ||
"camelcase": "^8.0.0", | ||
"conventional-changelog-cli": "^5.0.0", | ||
"happy-dom": "^14.7.1", | ||
"prettier": "^3.2.5", | ||
"rimraf": "^5.0.5", | ||
"rollup": "^2.79.1", | ||
"rollup-plugin-commonjs": "^10.1.0", | ||
"rollup-plugin-node-resolve": "^5.2.0", | ||
"rollup-plugin-terser": "^7.0.2", | ||
"rollup": "^4.17.2", | ||
"rollup-plugin-typescript2": "^0.36.0", | ||
"size-limit": "^8.2.6", | ||
"typescript": "^5.2.2", | ||
"vitest": "^0.34.6" | ||
"size-limit": "^11.1.2", | ||
"typedoc": "^0.25.13", | ||
"typescript": "^5.4.5", | ||
"vitest": "^1.6.0" | ||
}, | ||
@@ -104,0 +107,0 @@ "repository": { |
@@ -1,2 +0,2 @@ | ||
# mande [![build status](https://github.com/posva/mande/workflows/test/badge.svg?branch=main)](https://github.com/posva/mande/actions/workflows/test.yml?query=branch%3Amain) [![npm package](https://badgen.net/npm/v/mande)](https://www.npmjs.com/package/mande) [![coverage](https://badgen.net/codecov/c/github/posva/mande/master)](https://codecov.io/github/posva/mande) [![thanks](https://badgen.net/badge/thanks/♥/pink)](https://github.com/posva/thanks) | ||
# mande [![build status](https://github.com/posva/mande/workflows/test/badge.svg?branch=main)](https://github.com/posva/mande/actions/workflows/test.yml?query=branch%3Amain) [![npm package](https://badgen.net/npm/v/mande)](https://www.npmjs.com/package/mande) [![coverage](https://badgen.net/codecov/c/github/posva/mande/main)](https://codecov.io/github/posva/mande) [![thanks](https://badgen.net/badge/thanks/♥/pink)](https://github.com/posva/thanks) | ||
@@ -64,3 +64,3 @@ > Simple, light and extensible wrapper around fetch with smart defaults | ||
const users = mande('/api/users', globalOptions) | ||
const users = mande('/api/users', usersApiOptions) | ||
@@ -82,3 +82,3 @@ export function getUserById(id) { | ||
const todos = mande('/api/todos', globalOptions) | ||
const todos = mande('/api/todos', todosApiOptions) | ||
@@ -115,2 +115,22 @@ export function setToken(token) { | ||
To delete a header, pass `null` to the mande instance or the request: | ||
```ts | ||
const legacy = mande('/api/v1/data', { | ||
headers: { | ||
// override all requests | ||
'Content-Type': 'application/xml', | ||
}, | ||
}) | ||
// override only this request | ||
legacy.post(new FormData(), { | ||
headers: { | ||
// overrides Accept: 'application/json' only for this request | ||
Accept: null, | ||
'Content-Type': null, | ||
}, | ||
}) | ||
``` | ||
## TypeScript | ||
@@ -183,3 +203,3 @@ | ||
Most of the code can be discovered through the autocompletion but the API documentation is available at https://posva.net/mande/. | ||
Most of the code can be discovered through the autocompletion but the API documentation is available at [https://mande.esm.is](https://mande.esm.is) | ||
@@ -193,3 +213,3 @@ ### Cookbook | ||
```ts | ||
mande('/api').get('/users', { signal: AbortSignal.timeout(200) }) | ||
mande('/api').get('/users', { signal: AbortSignal.timeout(2000) }) | ||
``` | ||
@@ -199,2 +219,18 @@ | ||
#### `FormData` | ||
When passing [Form Data](https://developer.mozilla.org/en-US/docs/Web/API/FormData), mande automatically removes the `Content-Type` header but you can manually set it if needed: | ||
```ts | ||
// directly pass it to the mande instance | ||
const api = mande('/api/', { headers: { 'Content-Type': null } }) | ||
// or when creating the request | ||
const formData = new FormData() | ||
api.post(formData, { | ||
headers: { 'Content-Type': 'multipart/form-data' }, | ||
}) | ||
``` | ||
Most of the time you should let the browser set it for you. | ||
## Related | ||
@@ -201,0 +237,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
57627
912
238