Skip to content

Commit ba95295

Browse files
authored
Merge pull request #37 from zpencerq/custom_fetchoptions
Add extraFetchOptions fluent API to allow for per-call fetchOptions
2 parents e538837 + 0d0eeb4 commit ba95295

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

src/model.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -832,6 +832,13 @@ export class SpraypaintBase {
832832
return this.scope().extraParams(clause)
833833
}
834834

835+
static extraFetchOptions<I extends typeof SpraypaintBase>(
836+
this: I,
837+
options: RequestInit
838+
) {
839+
return this.scope().extraFetchOptions(options)
840+
}
841+
835842
static order<I extends typeof SpraypaintBase>(
836843
this: I,
837844
clause: SortScope | string

src/scope.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export class Scope<T extends SpraypaintBase = SpraypaintBase> {
5151
private _include: IncludeScopeHash = {}
5252
private _stats: StatsScope = {}
5353
private _extraParams: any = {}
54+
private _extraFetchOptions: RequestInit = {}
5455

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

216+
extraFetchOptions(options: RequestInit): Scope<T> {
217+
const copy = this.copy()
218+
219+
for (const key in options) {
220+
if (options.hasOwnProperty(key)) {
221+
copy._extraFetchOptions[key] = options[key]
222+
}
223+
}
224+
225+
return copy
226+
}
227+
215228
// The `Model` class has a `scope()` method to return the scope for it.
216229
// This method makes it possible for methods to expect either a model or
217230
// a scope and reliably cast them to a scope for use via `scope()`
@@ -247,6 +260,13 @@ export class Scope<T extends SpraypaintBase = SpraypaintBase> {
247260
}
248261
}
249262

263+
fetchOptions(): RequestInit {
264+
return {
265+
...this.model.fetchOptions(),
266+
...this._extraFetchOptions
267+
}
268+
}
269+
250270
copy(): Scope<T> {
251271
const newScope = cloneDeep(this)
252272

@@ -317,9 +337,7 @@ export class Scope<T extends SpraypaintBase = SpraypaintBase> {
317337
url = `${url}?${qp}`
318338
}
319339
const request = new Request(this.model.middlewareStack, this.model.logger)
320-
const fetchOpts = this.model.fetchOptions()
321-
322-
const response = await request.get(url, fetchOpts)
340+
const response = await request.get(url, this.fetchOptions())
323341
refreshJWT(this.model, response)
324342
return response.jsonPayload
325343
}

test/integration/finders.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,19 @@ describe("Model finders", () => {
2020
})
2121
})
2222

23+
it("allows for overridable fetch options", async () => {
24+
const dummyHeaders = { Test: "hi" }
25+
const { data } = await Person.extraFetchOptions({
26+
headers: dummyHeaders
27+
}).find(1)
28+
29+
const defaultFetchOptions = Person.fetchOptions
30+
31+
const lastOptions = fetchMock.lastOptions()
32+
33+
expect(lastOptions.headers).to.deep.eq(dummyHeaders)
34+
})
35+
2336
it("returns a promise that resolves the correct instance", async () => {
2437
const { data } = await Person.find(1)
2538
expect(data)

0 commit comments

Comments
 (0)