Skip to content

Commit

Permalink
Merge pull request #37 from zpencerq/custom_fetchoptions
Browse files Browse the repository at this point in the history
Add extraFetchOptions fluent API to allow for per-call fetchOptions
  • Loading branch information
richmolj authored Aug 21, 2020
2 parents e538837 + 0d0eeb4 commit ba95295
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
7 changes: 7 additions & 0 deletions src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,13 @@ export class SpraypaintBase {
return this.scope().extraParams(clause)
}

static extraFetchOptions<I extends typeof SpraypaintBase>(
this: I,
options: RequestInit
) {
return this.scope().extraFetchOptions(options)
}

static order<I extends typeof SpraypaintBase>(
this: I,
clause: SortScope | string
Expand Down
24 changes: 21 additions & 3 deletions src/scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export class Scope<T extends SpraypaintBase = SpraypaintBase> {
private _include: IncludeScopeHash = {}
private _stats: StatsScope = {}
private _extraParams: any = {}
private _extraFetchOptions: RequestInit = {}

constructor(model: Constructor<T> | typeof SpraypaintBase) {
this.model = (model as any) as typeof SpraypaintBase
Expand Down Expand Up @@ -212,6 +213,18 @@ export class Scope<T extends SpraypaintBase = SpraypaintBase> {
return copy
}

extraFetchOptions(options: RequestInit): Scope<T> {
const copy = this.copy()

for (const key in options) {
if (options.hasOwnProperty(key)) {
copy._extraFetchOptions[key] = options[key]
}
}

return copy
}

// The `Model` class has a `scope()` method to return the scope for it.
// This method makes it possible for methods to expect either a model or
// a scope and reliably cast them to a scope for use via `scope()`
Expand Down Expand Up @@ -247,6 +260,13 @@ export class Scope<T extends SpraypaintBase = SpraypaintBase> {
}
}

fetchOptions(): RequestInit {
return {
...this.model.fetchOptions(),
...this._extraFetchOptions
}
}

copy(): Scope<T> {
const newScope = cloneDeep(this)

Expand Down Expand Up @@ -317,9 +337,7 @@ export class Scope<T extends SpraypaintBase = SpraypaintBase> {
url = `${url}?${qp}`
}
const request = new Request(this.model.middlewareStack, this.model.logger)
const fetchOpts = this.model.fetchOptions()

const response = await request.get(url, fetchOpts)
const response = await request.get(url, this.fetchOptions())
refreshJWT(this.model, response)
return response.jsonPayload
}
Expand Down
13 changes: 13 additions & 0 deletions test/integration/finders.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,19 @@ describe("Model finders", () => {
})
})

it("allows for overridable fetch options", async () => {
const dummyHeaders = { Test: "hi" }
const { data } = await Person.extraFetchOptions({
headers: dummyHeaders
}).find(1)

const defaultFetchOptions = Person.fetchOptions

const lastOptions = fetchMock.lastOptions()

expect(lastOptions.headers).to.deep.eq(dummyHeaders)
})

it("returns a promise that resolves the correct instance", async () => {
const { data } = await Person.find(1)
expect(data)
Expand Down

0 comments on commit ba95295

Please sign in to comment.