Skip to content

Commit

Permalink
🔥 Add url arg and harmonize replace/mixin arguments
Browse files Browse the repository at this point in the history
BREAKING CHANGES:
- Replaces the argument to set fetch options with an argument to set url instead when calling http methods (.get/.post…)
- Removes the mixin arguments for all functions, now there is a replace argument instead everywhere. The logic when not providing the argument (default values) should stay the same.
  • Loading branch information
elbywan committed Jul 3, 2022
1 parent 9a0a387 commit 8e4d0aa
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 45 deletions.
50 changes: 25 additions & 25 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ export interface Wretch<Self = unknown, Chain = unknown> {
/**
* Returns a new Wretch object with the same url and new options.
* @param options - New options
* @param mixin - If true, mixes in instead of replacing the existing options
* @param replace - If true, replaces the existing options
*/
options(this: Self & Wretch<Self, Chain>, options: WretchOptions, mixin?: boolean): this
options(this: Self & Wretch<Self, Chain>, options: WretchOptions, replace?: boolean): this

/**
* Set request headers.
Expand Down Expand Up @@ -116,31 +116,31 @@ export interface Wretch<Self = unknown, Chain = unknown> {
/**
* Performs a get request.
*/
get(this: Self & Wretch<Self, Chain>, options?: WretchOptions): Chain & WretchResponseChain<Self, Chain>
get(this: Self & Wretch<Self, Chain>, url?: string, options?: WretchOptions): Chain & WretchResponseChain<Self, Chain>
/**
* Performs a delete request.
*/
delete(this: Self & Wretch<Self, Chain>, options?: WretchOptions): Chain & WretchResponseChain<Self, Chain>
delete(this: Self & Wretch<Self, Chain>, url?: string, options?: WretchOptions): Chain & WretchResponseChain<Self, Chain>
/**
* Performs a put request.
*/
put(this: Self & Wretch<Self, Chain>, body?: any, options?: WretchOptions): Chain & WretchResponseChain<Self, Chain>
put(this: Self & Wretch<Self, Chain>, body?: any, url?: string, options?: WretchOptions): Chain & WretchResponseChain<Self, Chain>
/**
* Performs a post request.
*/
post(this: Self & Wretch<Self, Chain>, body?: any, options?: WretchOptions): Chain & WretchResponseChain<Self, Chain>
post(this: Self & Wretch<Self, Chain>, body?: any, url?: string, options?: WretchOptions): Chain & WretchResponseChain<Self, Chain>
/**
* Performs a patch request.
*/
patch(this: Self & Wretch<Self, Chain>, body?: any, options?: WretchOptions): Chain & WretchResponseChain<Self, Chain>
patch(this: Self & Wretch<Self, Chain>, body?: any, url?: string, options?: WretchOptions): Chain & WretchResponseChain<Self, Chain>
/**
* Performs a head request.
*/
head(this: Self & Wretch<Self, Chain>, options?: WretchOptions): Chain & WretchResponseChain<Self, Chain>
head(this: Self & Wretch<Self, Chain>, url?: string, options?: WretchOptions): Chain & WretchResponseChain<Self, Chain>
/**
* Performs an options request
*/
opts(this: Self & Wretch<Self, Chain>, options?: WretchOptions): Chain & WretchResponseChain<Self, Chain>
opts(this: Self & Wretch<Self, Chain>, url?: string, options?: WretchOptions): Chain & WretchResponseChain<Self, Chain>
/**
* Replay a request.
*/
Expand Down Expand Up @@ -227,8 +227,8 @@ export const core: Wretch = {
this._url + url
})
},
options(options, mixin = true) {
return this.clone({ options: mixin ? mix(this._options, options) : options })
options(options, replace = false) {
return this.clone({ options: replace ? options : mix(this._options, options) })
},
headers(headerValues) {
return this.clone({ options: mix(this._options, { headers: headerValues || {} }) })
Expand Down Expand Up @@ -275,26 +275,26 @@ export const core: Wretch = {
.reduce((acc: Wretch, curr) => curr(acc, acc._url, acc._options), base)
)
},
get(options) {
return this.method("GET", options)
get(url = "", options) {
return this.url(url).method("GET", options)
},
delete(options) {
return this.method("DELETE", options)
delete(url = "", options) {
return this.url(url).method("DELETE", options)
},
put(body, options) {
return this.method("PUT", options, body)
put(body, url = "", options) {
return this.url(url).method("PUT", options, body)
},
post(body, options) {
return this.method("POST", options, body)
post(body, url = "", options) {
return this.url(url).method("POST", options, body)
},
patch(body, options) {
return this.method("PATCH", options, body)
patch(body, url = "", options) {
return this.url(url).method("PATCH", options, body)
},
head(options) {
return this.method("HEAD", options)
head(url = "", options) {
return this.url(url).method("HEAD", options)
},
opts(options) {
return this.method("OPTIONS", options)
opts(url = "", options) {
return this.url(url).method("OPTIONS", options)
},
replay(options) {
return this.method(this._options.method, options)
Expand Down
18 changes: 9 additions & 9 deletions test/browser/spec/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const _PORT = 9876
const _URL = 'http://localhost:' + _PORT

const allRoutes = (obj, type, action, opts, body) => Promise.all([
obj.get(opts)[type](_ => _).then(action),
obj.put(body, opts)[type](action),
obj.patch(body, opts)[type](action),
obj.post(body, opts)[type](action),
obj.delete(opts)[type](action),
const allRoutes = (obj, type, action, body) => Promise.all([
obj.get("")[type](_ => _).then(action),
obj.put(body, "")[type](action),
obj.patch(body, "")[type](action),
obj.post(body, "")[type](action),
obj.delete("")[type](action),
])

const isSafari =
Expand Down Expand Up @@ -132,11 +132,11 @@ describe("Wretch", function () {

it("should perform OPTIONS and HEAD requests", async function () {
const optsRes = await wretch(_URL + "/options").opts().res()
const optsRes2 = await wretch(_URL + "/options").opts({}).res()
const optsRes2 = await wretch(_URL + "/options").opts("").res()
expect(optsRes.headers.get("Allow")).toBe("OPTIONS")
expect(optsRes2.headers.get("Allow")).toBe("OPTIONS")
const headRes = await wretch(_URL + "/json").head().res()
const headRes2 = await wretch(_URL + "/json").head({}).res()
const headRes2 = await wretch(_URL + "/json").head("").res()
expect(headRes.headers.get("content-type")).toBe("application/json")
expect(headRes2.headers.get("content-type")).toBe("application/json")
})
Expand Down Expand Up @@ -465,7 +465,7 @@ describe("Wretch", function () {

const result = await w
.options({ token: "Basic d3JldGNoOnJvY2tz" })
.get({ q: "a" })
.get("", { q: "a" })
.text()
expect(result).toBe("ok")
})
Expand Down
Loading

0 comments on commit 8e4d0aa

Please sign in to comment.