Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,13 @@ class SimpleQL {
* Not the right solution
*/
private async processHeaders(
headers: ISimpleQLHeaders
headers: ISimpleQLHeaders,
): Promise<HeadersInit> {
const h: ISimpleQLHeaders = { ...headers } as DynamicHeaders
const keys = Object.keys(headers)
const dynamicHeaders = headers as DynamicHeaders

if (keys.length > 0 && keys.filter(String).length === keys.length) {
if (keys.length > 0 && keys.filter(String).length === keys.length) {
for (let name in headers) {
if (typeof dynamicHeaders[name] === 'function') {
h[name] = await (dynamicHeaders[name] as DynamicHeaderValue)()
Expand All @@ -115,10 +115,13 @@ class SimpleQL {

private async fetch<T>(
ctx: object | IQueryOptions,
options: IRequestInit
options: IRequestInit,
): Promise<IGraphQLResponse<T>> {
try {
const headers = await this.processHeaders(this.options.headers)
const headers = await this.processHeaders({
...this.options.headers,
...options.headers,
})

const res = await fetch(
this.options.method.toLocaleLowerCase() === 'get'
Expand All @@ -130,7 +133,7 @@ class SimpleQL {
? { body: this.prepareBody(ctx as IQueryOptions) }
: {}),
headers,
}
},
)
if (res.status >= 400) {
const error = await res.text()
Expand Down Expand Up @@ -173,7 +176,7 @@ class SimpleQL {
var key: any
for (key in params) {
query += `${encodeURIComponent(key)}=${encodeURIComponent(
this.encodeURIType(params[key])
this.encodeURIType(params[key]),
)}&`
}

Expand Down