@contember/graphql-client
Advanced tools
Comparing version 2.0.0-alpha.19 to 2.0.0-alpha.20
import { GraphQlClientError } from "./GraphQlClientError.js"; | ||
class GraphQlClient { | ||
constructor(apiUrl, apiToken) { | ||
this.apiUrl = apiUrl; | ||
this.apiToken = apiToken; | ||
constructor(options) { | ||
this.options = options; | ||
} | ||
get apiUrl() { | ||
return this.options.url; | ||
} | ||
withOptions(options) { | ||
return new GraphQlClient({ ...this.options, ...options }); | ||
} | ||
async execute(query, options = {}) { | ||
@@ -12,3 +17,3 @@ let body = null; | ||
const request = { | ||
url: this.apiUrl, | ||
url: this.options.url, | ||
query, | ||
@@ -25,2 +30,3 @@ variables: options.variables ?? {} | ||
}; | ||
this.options?.onBeforeRequest?.({ query, variables: options.variables ?? {} }); | ||
options?.onBeforeRequest?.({ query, variables: options.variables ?? {} }); | ||
@@ -33,2 +39,3 @@ try { | ||
} | ||
this.options?.onResponse?.(response); | ||
options?.onResponse?.(response); | ||
@@ -42,2 +49,3 @@ body = await response.text(); | ||
} | ||
this.options?.onData?.(data); | ||
options?.onData?.(data); | ||
@@ -70,9 +78,10 @@ if (response.status === 401) { | ||
"Content-Type": "application/json", | ||
...this.options.headers, | ||
...headers | ||
}; | ||
const resolvedToken = apiToken ?? this.apiToken; | ||
const resolvedToken = apiToken ?? this.options.apiToken; | ||
if (resolvedToken !== void 0) { | ||
resolvedHeaders["Authorization"] = `Bearer ${resolvedToken}`; | ||
} | ||
return await fetch(this.apiUrl, { | ||
return await (this.options.fetcher ?? defaultFetcher)(this.options.url, { | ||
method: "POST", | ||
@@ -85,2 +94,3 @@ headers: resolvedHeaders, | ||
} | ||
const defaultFetcher = async (url, options) => fetch(url, options); | ||
export { | ||
@@ -87,0 +97,0 @@ GraphQlClient |
import { GraphQlClientError } from "./GraphQlClientError.js"; | ||
class GraphQlClient { | ||
constructor(apiUrl, apiToken) { | ||
this.apiUrl = apiUrl; | ||
this.apiToken = apiToken; | ||
constructor(options) { | ||
this.options = options; | ||
} | ||
get apiUrl() { | ||
return this.options.url; | ||
} | ||
withOptions(options) { | ||
return new GraphQlClient({ ...this.options, ...options }); | ||
} | ||
async execute(query, options = {}) { | ||
@@ -12,3 +17,3 @@ let body = null; | ||
const request = { | ||
url: this.apiUrl, | ||
url: this.options.url, | ||
query, | ||
@@ -25,2 +30,3 @@ variables: options.variables ?? {} | ||
}; | ||
this.options?.onBeforeRequest?.({ query, variables: options.variables ?? {} }); | ||
options?.onBeforeRequest?.({ query, variables: options.variables ?? {} }); | ||
@@ -33,2 +39,3 @@ try { | ||
} | ||
this.options?.onResponse?.(response); | ||
options?.onResponse?.(response); | ||
@@ -42,2 +49,3 @@ body = await response.text(); | ||
} | ||
this.options?.onData?.(data); | ||
options?.onData?.(data); | ||
@@ -70,9 +78,10 @@ if (response.status === 401) { | ||
"Content-Type": "application/json", | ||
...this.options.headers, | ||
...headers | ||
}; | ||
const resolvedToken = apiToken ?? this.apiToken; | ||
const resolvedToken = apiToken ?? this.options.apiToken; | ||
if (resolvedToken !== void 0) { | ||
resolvedHeaders["Authorization"] = `Bearer ${resolvedToken}`; | ||
} | ||
return await fetch(this.apiUrl, { | ||
return await (this.options.fetcher ?? defaultFetcher)(this.options.url, { | ||
method: "POST", | ||
@@ -85,2 +94,3 @@ headers: resolvedHeaders, | ||
} | ||
const defaultFetcher = async (url, options) => fetch(url, options); | ||
export { | ||
@@ -87,0 +97,0 @@ GraphQlClient |
@@ -1,6 +0,7 @@ | ||
import { GraphQlClientRequestOptions } from './GraphQlClientRequestOptions'; | ||
import { GraphQlClientOptions, GraphQlClientRequestOptions } from './GraphQlClientRequestOptions'; | ||
export declare class GraphQlClient { | ||
readonly apiUrl: string; | ||
private readonly apiToken?; | ||
constructor(apiUrl: string, apiToken?: string | undefined); | ||
private readonly options; | ||
constructor(options: GraphQlClientOptions); | ||
get apiUrl(): string; | ||
withOptions(options: Partial<GraphQlClientOptions>): GraphQlClient; | ||
execute<T = unknown>(query: string, options?: GraphQlClientRequestOptions): Promise<T>; | ||
@@ -7,0 +8,0 @@ protected doExecute(query: string, { apiToken, signal, variables, headers }?: GraphQlClientRequestOptions): Promise<Response>; |
@@ -1,13 +0,19 @@ | ||
export interface GraphQlClientRequestOptions { | ||
variables?: GraphQlClientVariables; | ||
apiToken?: string; | ||
signal?: AbortSignal; | ||
headers?: Record<string, string>; | ||
onBeforeRequest?: (query: { | ||
export interface GraphQlClientBaseOptions { | ||
readonly apiToken?: string; | ||
readonly headers?: Record<string, string>; | ||
readonly onBeforeRequest?: (query: { | ||
query: string; | ||
variables: GraphQlClientVariables; | ||
}) => void; | ||
onResponse?: (response: Response) => void; | ||
onData?: (json: unknown) => void; | ||
readonly onResponse?: (response: Response) => void; | ||
readonly onData?: (json: unknown) => void; | ||
} | ||
export interface GraphQlClientOptions extends GraphQlClientBaseOptions { | ||
readonly url: string; | ||
readonly fetcher?: (input: RequestInfo, init?: RequestInit) => Promise<Response>; | ||
} | ||
export interface GraphQlClientRequestOptions extends GraphQlClientBaseOptions { | ||
readonly variables?: GraphQlClientVariables; | ||
readonly signal?: AbortSignal; | ||
} | ||
export interface GraphQlClientVariables { | ||
@@ -14,0 +20,0 @@ [name: string]: any; |
{ | ||
"name": "@contember/graphql-client", | ||
"license": "Apache-2.0", | ||
"version": "2.0.0-alpha.19", | ||
"version": "2.0.0-alpha.20", | ||
"main": "./dist/production/index.js", | ||
@@ -6,0 +6,0 @@ "exports": { |
import { GraphQlClientError, GraphQlErrorType } from './GraphQlClientError' | ||
import { GraphQlClientRequestOptions } from './GraphQlClientRequestOptions' | ||
import { GraphQlClientOptions, GraphQlClientRequestOptions } from './GraphQlClientRequestOptions' | ||
export class GraphQlClient { | ||
constructor(public readonly apiUrl: string, private readonly apiToken?: string) { } | ||
constructor( | ||
private readonly options: GraphQlClientOptions, | ||
) { } | ||
get apiUrl(): string { | ||
return this.options.url | ||
} | ||
withOptions(options: Partial<GraphQlClientOptions>): GraphQlClient { | ||
return new GraphQlClient({ ...this.options, ...options }) | ||
} | ||
async execute<T = unknown>(query: string, options: GraphQlClientRequestOptions = {}): Promise<T> { | ||
@@ -12,3 +22,3 @@ let body: string | null = null | ||
const request = { | ||
url: this.apiUrl, | ||
url: this.options.url, | ||
query, | ||
@@ -28,2 +38,3 @@ variables: options.variables ?? {}, | ||
this.options?.onBeforeRequest?.({ query, variables: options.variables ?? {} }) | ||
options?.onBeforeRequest?.({ query, variables: options.variables ?? {} }) | ||
@@ -38,2 +49,3 @@ | ||
this.options?.onResponse?.(response) | ||
options?.onResponse?.(response) | ||
@@ -49,2 +61,3 @@ | ||
} | ||
this.options?.onData?.(data) | ||
options?.onData?.(data) | ||
@@ -84,5 +97,6 @@ | ||
'Content-Type': 'application/json', | ||
...this.options.headers, | ||
...headers, | ||
} | ||
const resolvedToken = apiToken ?? this.apiToken | ||
const resolvedToken = apiToken ?? this.options.apiToken | ||
@@ -93,3 +107,3 @@ if (resolvedToken !== undefined) { | ||
return await fetch(this.apiUrl, { | ||
return await (this.options.fetcher ?? defaultFetcher)(this.options.url, { | ||
method: 'POST', | ||
@@ -103,1 +117,2 @@ headers: resolvedHeaders, | ||
const defaultFetcher = async (url: string, options: RequestInit) => fetch(url, options) |
@@ -1,13 +0,21 @@ | ||
export interface GraphQlClientRequestOptions { | ||
variables?: GraphQlClientVariables | ||
apiToken?: string | ||
signal?: AbortSignal | ||
headers?: Record<string, string> | ||
onBeforeRequest?: (query: { query: string; variables: GraphQlClientVariables }) => void | ||
onResponse?: (response: Response) => void | ||
onData?: (json: unknown) => void | ||
export interface GraphQlClientBaseOptions { | ||
readonly apiToken?: string | ||
readonly headers?: Record<string, string> | ||
readonly onBeforeRequest?: (query: { query: string; variables: GraphQlClientVariables }) => void | ||
readonly onResponse?: (response: Response) => void | ||
readonly onData?: (json: unknown) => void | ||
} | ||
export interface GraphQlClientOptions extends GraphQlClientBaseOptions { | ||
readonly url: string | ||
readonly fetcher?: (input: RequestInfo, init?: RequestInit) => Promise<Response> | ||
} | ||
export interface GraphQlClientRequestOptions extends GraphQlClientBaseOptions{ | ||
readonly variables?: GraphQlClientVariables | ||
readonly signal?: AbortSignal | ||
} | ||
export interface GraphQlClientVariables { | ||
[name: string]: any | ||
} |
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
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
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
107200
679