@hyper-fetch/graphql 7.5.2 → 8.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/{.eslintrc.json → .eslintrc copy.json } +1 -1
- package/.eslintrc.cjs +12 -0
- package/__tests__/constants/mutations.constants.ts +15 -0
- package/__tests__/constants/queries.constants.ts +24 -0
- package/__tests__/e2e/e2e.graphql-server.ts +135 -0
- package/__tests__/e2e/e2e.graphql.spec.ts +486 -0
- package/__tests__/features/adapter/adapter.browser.spec.ts +259 -0
- package/__tests__/features/adapter/adapter.server.spec.ts +234 -0
- package/__tests__/features/adapter/adapter.utils.spec.ts +86 -0
- package/__tests__/request-module.d.ts +12 -0
- package/__tests__/shared/callbacks.shared.ts +11 -0
- package/__tests__/shared/index.ts +2 -0
- package/__tests__/shared/progress.shared.ts +40 -0
- package/__tests__/tsconfig.json +23 -0
- package/__tests__/vitest.setup.ts +10 -0
- package/dist/adapter/adapter.constants.d.ts +4 -0
- package/dist/adapter/adapter.constants.d.ts.map +1 -0
- package/dist/adapter/adapter.d.ts +3 -0
- package/dist/adapter/adapter.d.ts.map +1 -0
- package/dist/adapter/adapter.types.d.ts +24 -0
- package/dist/adapter/adapter.types.d.ts.map +1 -0
- package/dist/adapter/adapter.utils.d.ts +9 -0
- package/dist/adapter/adapter.utils.d.ts.map +1 -0
- package/dist/adapter/http-adapter.fetch.d.ts +3 -0
- package/dist/adapter/http-adapter.fetch.d.ts.map +1 -0
- package/dist/adapter/index.d.ts +6 -0
- package/dist/adapter/index.d.ts.map +1 -0
- package/dist/index.d.ts +2 -34
- package/dist/index.d.ts.map +1 -0
- package/dist/index.mjs +183 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +10 -25
- package/project.json +4 -0
- package/src/adapter/adapter.constants.ts +1 -1
- package/src/adapter/adapter.ts +3 -8
- package/src/adapter/adapter.types.ts +7 -3
- package/src/adapter/adapter.utils.ts +8 -3
- package/src/adapter/http-adapter.fetch.ts +186 -0
- package/src/adapter/index.ts +1 -0
- package/tsconfig.base.json +2 -6
- package/tsconfig.json +2 -6
- package/vite.config.ts +45 -0
- package/dist/browser/index.js +0 -191
- package/dist/browser/index.js.map +0 -7
- package/dist/index.js +0 -330
- package/dist/index.js.map +0 -7
- package/src/adapter/http-adapter.browser.ts +0 -148
- package/src/adapter/http-adapter.server.ts +0 -148
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"adapter.constants.d.ts","sourceRoot":"","sources":["../../src/adapter/adapter.constants.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAC;AAExD,eAAO,MAAM,cAAc,QAAe,CAAC;AAE3C,eAAO,MAAM,QAAQ,EAAE,gBAGtB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"adapter.d.ts","sourceRoot":"","sources":["../../src/adapter/adapter.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AAE1D,eAAO,MAAM,cAAc,QAAO,kBAEjC,CAAC"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { Adapter, EmptyTypes, QueryParamsType } from '@hyper-fetch/core';
|
|
2
|
+
import { DocumentNode } from 'graphql/language/ast';
|
|
3
|
+
export declare enum GraphqlMethod {
|
|
4
|
+
POST = "POST",
|
|
5
|
+
GET = "GET"
|
|
6
|
+
}
|
|
7
|
+
export type GraphQlExtraType = {
|
|
8
|
+
headers: HeadersInit;
|
|
9
|
+
extensions: Record<string, any>;
|
|
10
|
+
};
|
|
11
|
+
export type GraphQlEndpointType = string | DocumentNode;
|
|
12
|
+
export type FetchGraphqlAdapterOptionsType = Omit<RequestInit, "method" | "headers" | "body" | "signal"> & {
|
|
13
|
+
timeout?: number;
|
|
14
|
+
};
|
|
15
|
+
export type GraphqlAdapterType = Adapter<FetchGraphqlAdapterOptionsType, GraphqlMethod, number, GraphQlExtraType, QueryParamsType | string, undefined, GraphQlEndpointType, (endpoint: GraphQlEndpointType) => string>;
|
|
16
|
+
type GqlParamType = boolean | string | number | Record<string, any> | EmptyTypes;
|
|
17
|
+
export type ExtractGqlRouteParams<T extends string> = string extends T ? EmptyTypes : T extends `${string}query ${infer Parameters} {${infer QueryRest}` ? Parameters extends `${string}$${infer Param},${infer Rest}` ? GqlParameter<Param, Rest> & (ExtractGqlRouteParams<QueryRest> extends EmptyTypes ? {} : ExtractGqlRouteParams<QueryRest>) : Parameters extends `${string}$${infer Param})${infer Rest}` ? GqlParameter<Param, Rest> & (ExtractGqlRouteParams<QueryRest> extends EmptyTypes ? {} : ExtractGqlRouteParams<QueryRest>) : EmptyTypes : T extends `${string}$${infer Parameters},${infer Rest}` ? GqlParameter<Parameters, Rest> : T extends `${string}$${infer Parameters})${infer Rest}` ? GqlParameter<Parameters, Rest> : EmptyTypes;
|
|
18
|
+
export type GqlParameter<T extends string, Rest extends string> = T extends `${infer Param}:${string}!` ? {
|
|
19
|
+
[k in Param]: GqlParamType;
|
|
20
|
+
} & (ExtractGqlRouteParams<Rest> extends EmptyTypes ? {} : ExtractGqlRouteParams<Rest>) : T extends `${infer Param}:${string}` ? {
|
|
21
|
+
[k in Param]?: GqlParamType;
|
|
22
|
+
} & (ExtractGqlRouteParams<Rest> extends EmptyTypes ? {} : ExtractGqlRouteParams<Rest>) : EmptyTypes;
|
|
23
|
+
export {};
|
|
24
|
+
//# sourceMappingURL=adapter.types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"adapter.types.d.ts","sourceRoot":"","sources":["../../src/adapter/adapter.types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,UAAU,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAC9E,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AAEzD,oBAAY,aAAa;IACvB,IAAI,SAAS;IACb,GAAG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E,GAAG,qBAAqB,CAAC,SAAS,CAAC,CAAC,GAC/F,UAAU,GACd,CAAC,SAAS,GAAG,MAAM,IAAI,MAAM,UAAU,IAAI,MAAM,IAAI,EAAE,GACrD,YAAY,CAAC,UAAU,EAAE,IAAI,CAAC,GAC9B,CAAC,SAAS,GAAG,MAAM,IAAI,MAAM,UAAU,IAAI,MAAM,IAAI,EAAE,GACrD,YAAY,CAAC,UAAU,EAAE,IAAI,CAAC,GAC9B,UAAU,CAAC;AAErB,MAAM,MAAM,YAAY,CAAC,CAAC,SAAS,MAAM,EAAE,IAAI,SAAS,MAAM,IAAI,CAAC,SAAS,GAAG,MAAM,KAAK,IAAI,MAAM,GAAG,GACnG;KAAG,CAAC,IAAI,KAAK,GAAG,YAAY;CAAE,GAAG,CAAC,qBAAqB,CAAC,IAAI,CAAC,SAAS,UAAU,GAE5E,EAAE,GACF,qBAAqB,CAAC,IAAI,CAAC,CAAC,GAChC,CAAC,SAAS,GAAG,MAAM,KAAK,IAAI,MAAM,EAAE,GAClC;KAAG,CAAC,IAAI,KAAK,CAAC,CAAC,EAAE,YAAY;CAAE,GAAG,CAAC,qBAAqB,CAAC,IAAI,CAAC,SAAS,UAAU,GAE7E,EAAE,GACF,qBAAqB,CAAC,IAAI,CAAC,CAAC,GAChC,UAAU,CAAC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { RequestInstance } from '@hyper-fetch/core';
|
|
2
|
+
import { GraphQlEndpointType } from '.';
|
|
3
|
+
export declare const getRequestValues: <R extends RequestInstance>(request: R) => {
|
|
4
|
+
fullUrl: string;
|
|
5
|
+
payload: string | null;
|
|
6
|
+
};
|
|
7
|
+
export declare const gqlEndpointMapper: <E extends GraphQlEndpointType>(endpoint: E) => string;
|
|
8
|
+
export declare const gqlEndpointNameMapper: (endpoint: string) => string;
|
|
9
|
+
//# sourceMappingURL=adapter.utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"adapter.utils.d.ts","sourceRoot":"","sources":["../../src/adapter/adapter.utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAKzD,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,SAAS,CAAC;AAEnD,eAAO,MAAM,gBAAgB,GAAI,CAAC,SAAS,eAAe,EAAE,SAAS,CAAC;;;CAgBrE,CAAC;AAEF,eAAO,MAAM,iBAAiB,GAAI,CAAC,SAAS,mBAAmB,EAAE,UAAU,CAAC,KAAG,MAK9E,CAAC;AAEF,eAAO,MAAM,qBAAqB,GAAI,UAAU,MAAM,KAAG,MAgCxD,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"http-adapter.fetch.d.ts","sourceRoot":"","sources":["../../src/adapter/http-adapter.fetch.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EACV,kBAAkB,EAInB,MAAM,SAAS,CAAC;AAUjB,eAAO,MAAM,aAAa,QAAO,kBAuK5B,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/adapter/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAC;AAC1B,cAAc,iBAAiB,CAAC;AAChC,cAAc,iBAAiB,CAAC;AAChC,cAAc,qBAAqB,CAAC;AACpC,cAAc,sBAAsB,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,34 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
declare enum GraphqlMethod {
|
|
5
|
-
POST = "POST",
|
|
6
|
-
GET = "GET"
|
|
7
|
-
}
|
|
8
|
-
type GraphQlExtraType = {
|
|
9
|
-
headers: HeadersInit;
|
|
10
|
-
extensions: Record<string, any>;
|
|
11
|
-
};
|
|
12
|
-
type GraphQlEndpointType = string | DocumentNode;
|
|
13
|
-
type GraphqlAdapterType = Adapter<Partial<XMLHttpRequest>, GraphqlMethod, number, GraphQlExtraType, QueryParamsType | string, undefined, GraphQlEndpointType, (endpoint: GraphQlEndpointType) => string>;
|
|
14
|
-
type GqlParamType = boolean | string | number | Record<string, any> | EmptyTypes;
|
|
15
|
-
type ExtractGqlRouteParams<T extends string> = string extends T ? EmptyTypes : T extends `${string}query ${infer Parameters} {${infer QueryRest}` ? Parameters extends `${string}$${infer Param},${infer Rest}` ? GqlParameter<Param, Rest> & (ExtractGqlRouteParams<QueryRest> extends EmptyTypes ? {} : ExtractGqlRouteParams<QueryRest>) : Parameters extends `${string}$${infer Param})${infer Rest}` ? GqlParameter<Param, Rest> & (ExtractGqlRouteParams<QueryRest> extends EmptyTypes ? {} : ExtractGqlRouteParams<QueryRest>) : EmptyTypes : T extends `${string}$${infer Parameters},${infer Rest}` ? GqlParameter<Parameters, Rest> : T extends `${string}$${infer Parameters})${infer Rest}` ? GqlParameter<Parameters, Rest> : EmptyTypes;
|
|
16
|
-
type GqlParameter<T extends string, Rest extends string> = T extends `${infer Param}:${string}!` ? {
|
|
17
|
-
[k in Param]: GqlParamType;
|
|
18
|
-
} & (ExtractGqlRouteParams<Rest> extends EmptyTypes ? {} : ExtractGqlRouteParams<Rest>) : T extends `${infer Param}:${string}` ? {
|
|
19
|
-
[k in Param]?: GqlParamType;
|
|
20
|
-
} & (ExtractGqlRouteParams<Rest> extends EmptyTypes ? {} : ExtractGqlRouteParams<Rest>) : EmptyTypes;
|
|
21
|
-
|
|
22
|
-
declare const GraphqlAdapter: () => GraphqlAdapterType;
|
|
23
|
-
|
|
24
|
-
declare const getRequestValues: <R extends RequestInstance>(request: R) => {
|
|
25
|
-
fullUrl: string;
|
|
26
|
-
payload: string | null;
|
|
27
|
-
};
|
|
28
|
-
declare const gqlEndpointMapper: <E extends GraphQlEndpointType>(endpoint: E) => string;
|
|
29
|
-
declare const gqlEndpointNameMapper: (endpoint: string) => string;
|
|
30
|
-
|
|
31
|
-
declare const defaultTimeout: number;
|
|
32
|
-
declare const gqlExtra: GraphQlExtraType;
|
|
33
|
-
|
|
34
|
-
export { type ExtractGqlRouteParams, type GqlParameter, type GraphQlEndpointType, type GraphQlExtraType, GraphqlAdapter, type GraphqlAdapterType, GraphqlMethod, defaultTimeout, getRequestValues, gqlEndpointMapper, gqlEndpointNameMapper, gqlExtra };
|
|
1
|
+
export * from './adapter';
|
|
2
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,cAAc,WAAW,CAAC"}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import { Adapter, HttpMethods, Time, getErrorMessage, parseResponse, stringifyKey, stringifyQueryParams } from "@hyper-fetch/core";
|
|
2
|
+
import { parse, print } from "graphql";
|
|
3
|
+
//#region src/adapter/http-adapter.fetch.ts
|
|
4
|
+
var getGqlAdapter = () => new Adapter({
|
|
5
|
+
name: "graphql",
|
|
6
|
+
defaultMethod: GraphqlMethod.POST,
|
|
7
|
+
defaultExtra: gqlExtra,
|
|
8
|
+
systemErrorStatus: 0,
|
|
9
|
+
systemErrorExtra: gqlExtra
|
|
10
|
+
}).onInitialize(({ client }) => {
|
|
11
|
+
client.setCacheKeyMapper((request) => {
|
|
12
|
+
return `${request.method}_${gqlEndpointNameMapper(request.endpoint)}-${stringifyKey(request.params)}-${stringifyKey(request.queryParams)}`;
|
|
13
|
+
});
|
|
14
|
+
client.setQueryKeyMapper((request) => {
|
|
15
|
+
return `${request.method}_${gqlEndpointNameMapper(request.endpoint)}-${stringifyKey(request.params)}-${stringifyKey(request.queryParams)}`;
|
|
16
|
+
});
|
|
17
|
+
client.setAbortKeyMapper((request) => {
|
|
18
|
+
return `${request.method}_${gqlEndpointNameMapper(request.endpoint)}-${request.cancelable}`;
|
|
19
|
+
});
|
|
20
|
+
}).setInternalErrorMapping((error) => [error]).setDevtoolsEndpointGetter(gqlEndpointNameMapper).setEndpointMapper(gqlEndpointMapper).setFetcher(async ({ request, adapterOptions, headers, onError, onResponseEnd, onTimeoutError, onRequestEnd, createAbortListener, getAbortController, onResponseProgress, onResponseStart, onBeforeRequest, onRequestStart, onRequestProgress, onSuccess }) => {
|
|
21
|
+
const { fullUrl, payload } = getRequestValues(request);
|
|
22
|
+
const controller = getAbortController();
|
|
23
|
+
const { timeout: timeoutMs = defaultTimeout, ...restOptions } = adapterOptions || {};
|
|
24
|
+
let timedOut = false;
|
|
25
|
+
let timeoutId;
|
|
26
|
+
const unmountListener = createAbortListener({
|
|
27
|
+
status: 0,
|
|
28
|
+
extra: gqlExtra
|
|
29
|
+
});
|
|
30
|
+
if (timeoutMs > 0 && controller) timeoutId = setTimeout(() => {
|
|
31
|
+
timedOut = true;
|
|
32
|
+
unmountListener();
|
|
33
|
+
controller.abort();
|
|
34
|
+
}, timeoutMs);
|
|
35
|
+
onBeforeRequest();
|
|
36
|
+
onRequestStart();
|
|
37
|
+
try {
|
|
38
|
+
const init = {
|
|
39
|
+
method: request.method,
|
|
40
|
+
headers,
|
|
41
|
+
signal: controller?.signal,
|
|
42
|
+
...restOptions
|
|
43
|
+
};
|
|
44
|
+
if (payload) {
|
|
45
|
+
init.body = payload;
|
|
46
|
+
if (typeof payload === "string") {
|
|
47
|
+
const total = new Blob([payload]).size;
|
|
48
|
+
onRequestProgress({
|
|
49
|
+
total,
|
|
50
|
+
loaded: total
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
onRequestEnd();
|
|
55
|
+
const response = await fetch(fullUrl, init);
|
|
56
|
+
if (timeoutId) clearTimeout(timeoutId);
|
|
57
|
+
onResponseStart();
|
|
58
|
+
const responseHeaders = {};
|
|
59
|
+
response.headers.forEach((value, key) => {
|
|
60
|
+
responseHeaders[key] = value;
|
|
61
|
+
});
|
|
62
|
+
let body;
|
|
63
|
+
if (response.body && typeof response.body.getReader === "function") {
|
|
64
|
+
const reader = response.body.getReader();
|
|
65
|
+
const contentLength = parseInt(response.headers.get("content-length") || "0", 10);
|
|
66
|
+
let receivedLength = 0;
|
|
67
|
+
const chunks = [];
|
|
68
|
+
while (true) {
|
|
69
|
+
const { done, value } = await reader.read();
|
|
70
|
+
if (done) break;
|
|
71
|
+
chunks.push(value);
|
|
72
|
+
receivedLength += value.length;
|
|
73
|
+
onResponseProgress({
|
|
74
|
+
total: contentLength || receivedLength,
|
|
75
|
+
loaded: receivedLength
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
const allChunks = new Uint8Array(receivedLength);
|
|
79
|
+
let position = 0;
|
|
80
|
+
for (const chunk of chunks) {
|
|
81
|
+
allChunks.set(chunk, position);
|
|
82
|
+
position += chunk.length;
|
|
83
|
+
}
|
|
84
|
+
body = new TextDecoder().decode(allChunks);
|
|
85
|
+
} else body = await response.text();
|
|
86
|
+
onResponseEnd();
|
|
87
|
+
unmountListener();
|
|
88
|
+
const { status } = response;
|
|
89
|
+
const parsed = parseResponse(body);
|
|
90
|
+
const data = parsed?.data ?? null;
|
|
91
|
+
const errors = parsed?.errors || null;
|
|
92
|
+
const extensions = parsed?.extensions || {};
|
|
93
|
+
if (errors || status > 399 || status === 0) onError({
|
|
94
|
+
error: errors || [getErrorMessage()],
|
|
95
|
+
status,
|
|
96
|
+
extra: {
|
|
97
|
+
headers: responseHeaders,
|
|
98
|
+
extensions
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
else onSuccess({
|
|
102
|
+
data,
|
|
103
|
+
error: errors,
|
|
104
|
+
status,
|
|
105
|
+
extra: {
|
|
106
|
+
headers: responseHeaders,
|
|
107
|
+
extensions
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
} catch (err) {
|
|
111
|
+
if (timeoutId) clearTimeout(timeoutId);
|
|
112
|
+
unmountListener();
|
|
113
|
+
if (controller?.signal?.aborted) {
|
|
114
|
+
if (timedOut) onTimeoutError({
|
|
115
|
+
status: 0,
|
|
116
|
+
extra: gqlExtra
|
|
117
|
+
});
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
onError({
|
|
121
|
+
error: err,
|
|
122
|
+
status: 0,
|
|
123
|
+
extra: gqlExtra
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
//#endregion
|
|
128
|
+
//#region src/adapter/adapter.ts
|
|
129
|
+
var GraphqlAdapter = () => {
|
|
130
|
+
return getGqlAdapter();
|
|
131
|
+
};
|
|
132
|
+
//#endregion
|
|
133
|
+
//#region src/adapter/adapter.types.ts
|
|
134
|
+
var GraphqlMethod = /* @__PURE__ */ function(GraphqlMethod) {
|
|
135
|
+
GraphqlMethod["POST"] = "POST";
|
|
136
|
+
GraphqlMethod["GET"] = "GET";
|
|
137
|
+
return GraphqlMethod;
|
|
138
|
+
}({});
|
|
139
|
+
//#endregion
|
|
140
|
+
//#region src/adapter/adapter.utils.ts
|
|
141
|
+
var getRequestValues = (request) => {
|
|
142
|
+
const isPostRequest = request.method === HttpMethods.POST;
|
|
143
|
+
const query = request.endpoint;
|
|
144
|
+
const variables = request.payload;
|
|
145
|
+
return {
|
|
146
|
+
fullUrl: isPostRequest ? request.client.url : request.client.url + stringifyQueryParams({
|
|
147
|
+
query,
|
|
148
|
+
variables
|
|
149
|
+
}),
|
|
150
|
+
payload: isPostRequest ? JSON.stringify({
|
|
151
|
+
query,
|
|
152
|
+
variables
|
|
153
|
+
}) : null
|
|
154
|
+
};
|
|
155
|
+
};
|
|
156
|
+
var gqlEndpointMapper = (endpoint) => {
|
|
157
|
+
if (typeof endpoint === "string") return endpoint;
|
|
158
|
+
return print(endpoint);
|
|
159
|
+
};
|
|
160
|
+
var gqlEndpointNameMapper = (endpoint) => {
|
|
161
|
+
try {
|
|
162
|
+
const operationDefinition = parse(endpoint).definitions.find((def) => def.kind === "OperationDefinition");
|
|
163
|
+
if (operationDefinition) {
|
|
164
|
+
if (operationDefinition.name?.value) return operationDefinition.name.value;
|
|
165
|
+
const selection = operationDefinition.selectionSet.selections[0];
|
|
166
|
+
if (selection?.kind === "Field") return selection.name.value;
|
|
167
|
+
if (selection?.kind === "InlineFragment" && selection.typeCondition) return selection.typeCondition.name.value;
|
|
168
|
+
}
|
|
169
|
+
} catch (error) {}
|
|
170
|
+
const [name] = endpoint.replace(/(query|mutation|subscription)/, "").replace(/[{}().,:]/g, " ").trim().split(" ");
|
|
171
|
+
return name || "graphql-request";
|
|
172
|
+
};
|
|
173
|
+
//#endregion
|
|
174
|
+
//#region src/adapter/adapter.constants.ts
|
|
175
|
+
var defaultTimeout = Time.SEC * 5;
|
|
176
|
+
var gqlExtra = {
|
|
177
|
+
headers: {},
|
|
178
|
+
extensions: {}
|
|
179
|
+
};
|
|
180
|
+
//#endregion
|
|
181
|
+
export { GraphqlAdapter, GraphqlMethod, defaultTimeout, getGqlAdapter, getRequestValues, gqlEndpointMapper, gqlEndpointNameMapper, gqlExtra };
|
|
182
|
+
|
|
183
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../src/adapter/http-adapter.fetch.ts","../src/adapter/adapter.ts","../src/adapter/adapter.types.ts","../src/adapter/adapter.utils.ts","../src/adapter/adapter.constants.ts"],"sourcesContent":["import type { QueryParamsType } from \"@hyper-fetch/core\";\nimport { parseResponse, getErrorMessage, Adapter, stringifyKey } from \"@hyper-fetch/core\";\n\nimport type {\n GraphqlAdapterType,\n GraphQlExtraType,\n GraphQlEndpointType,\n FetchGraphqlAdapterOptionsType,\n} from \"adapter\";\nimport {\n gqlExtra,\n defaultTimeout,\n getRequestValues,\n GraphqlMethod,\n gqlEndpointMapper,\n gqlEndpointNameMapper,\n} from \"adapter\";\n\nexport const getGqlAdapter = (): GraphqlAdapterType =>\n new Adapter<\n FetchGraphqlAdapterOptionsType,\n GraphqlMethod,\n number,\n GraphQlExtraType,\n QueryParamsType | string,\n undefined,\n GraphQlEndpointType\n >({\n name: \"graphql\",\n defaultMethod: GraphqlMethod.POST,\n defaultExtra: gqlExtra,\n systemErrorStatus: 0,\n systemErrorExtra: gqlExtra,\n })\n .onInitialize(({ client }) => {\n client.setCacheKeyMapper((request) => {\n return `${request.method}_${gqlEndpointNameMapper(request.endpoint)}-${stringifyKey(request.params)}-${stringifyKey(request.queryParams)}`;\n });\n client.setQueryKeyMapper((request) => {\n return `${request.method}_${gqlEndpointNameMapper(request.endpoint)}-${stringifyKey(request.params)}-${stringifyKey(request.queryParams)}`;\n });\n client.setAbortKeyMapper((request) => {\n return `${request.method}_${gqlEndpointNameMapper(request.endpoint)}-${request.cancelable}`;\n });\n })\n .setInternalErrorMapping((error) => [error])\n .setDevtoolsEndpointGetter(gqlEndpointNameMapper)\n .setEndpointMapper(gqlEndpointMapper)\n .setFetcher(\n async ({\n request,\n adapterOptions,\n headers,\n onError,\n onResponseEnd,\n onTimeoutError,\n onRequestEnd,\n createAbortListener,\n getAbortController,\n onResponseProgress,\n onResponseStart,\n onBeforeRequest,\n onRequestStart,\n onRequestProgress,\n onSuccess,\n }) => {\n const { fullUrl, payload } = getRequestValues(request);\n\n const controller = getAbortController();\n const { timeout: timeoutMs = defaultTimeout, ...restOptions } = adapterOptions || {};\n\n let timedOut = false;\n let timeoutId: ReturnType<typeof setTimeout> | undefined;\n\n const unmountListener = createAbortListener({\n status: 0,\n extra: gqlExtra,\n });\n\n if (timeoutMs > 0 && controller) {\n timeoutId = setTimeout(() => {\n timedOut = true;\n unmountListener();\n controller.abort();\n }, timeoutMs);\n }\n\n onBeforeRequest();\n onRequestStart();\n\n try {\n const init: RequestInit = {\n method: request.method,\n headers: headers as Record<string, string>,\n signal: controller?.signal,\n ...restOptions,\n };\n\n if (payload) {\n init.body = payload;\n\n if (typeof payload === \"string\") {\n const total = new Blob([payload]).size;\n onRequestProgress({ total, loaded: total } as ProgressEvent);\n }\n }\n\n onRequestEnd();\n\n const response = await fetch(fullUrl, init);\n\n if (timeoutId) clearTimeout(timeoutId);\n\n onResponseStart();\n\n const responseHeaders: Record<string, string> = {};\n response.headers.forEach((value, key) => {\n responseHeaders[key] = value;\n });\n\n let body: string;\n\n if (response.body && typeof response.body.getReader === \"function\") {\n const reader = response.body.getReader();\n const contentLength = parseInt(response.headers.get(\"content-length\") || \"0\", 10);\n let receivedLength = 0;\n const chunks: Uint8Array[] = [];\n\n // eslint-disable-next-line no-constant-condition\n while (true) {\n // eslint-disable-next-line no-await-in-loop\n const { done, value } = await reader.read();\n if (done) break;\n chunks.push(value);\n receivedLength += value.length;\n onResponseProgress({ total: contentLength || receivedLength, loaded: receivedLength } as ProgressEvent);\n }\n\n const allChunks = new Uint8Array(receivedLength);\n let position = 0;\n // eslint-disable-next-line no-restricted-syntax\n for (const chunk of chunks) {\n allChunks.set(chunk, position);\n position += chunk.length;\n }\n body = new TextDecoder().decode(allChunks);\n } else {\n body = await response.text();\n }\n\n onResponseEnd();\n unmountListener();\n\n const { status } = response;\n const parsed = parseResponse(body);\n const data = parsed?.data ?? null;\n const errors = parsed?.errors || null;\n const extensions = parsed?.extensions || {};\n const failure = errors || status > 399 || status === 0;\n\n if (failure) {\n const error = errors || [getErrorMessage()];\n onError({ error, status, extra: { headers: responseHeaders, extensions } });\n } else {\n onSuccess({\n data,\n error: errors,\n status,\n extra: { headers: responseHeaders, extensions },\n });\n }\n } catch (err: any) {\n if (timeoutId) clearTimeout(timeoutId);\n unmountListener();\n\n if (controller?.signal?.aborted) {\n if (timedOut) {\n onTimeoutError({ status: 0, extra: gqlExtra });\n }\n return;\n }\n\n onError({ error: err, status: 0, extra: gqlExtra });\n }\n },\n );\n","import { getGqlAdapter } from \"./http-adapter.fetch\";\nimport type { GraphqlAdapterType } from \"./adapter.types\";\n\nexport const GraphqlAdapter = (): GraphqlAdapterType => {\n return getGqlAdapter();\n};\n","import type { Adapter, EmptyTypes, QueryParamsType } from \"@hyper-fetch/core\";\nimport type { DocumentNode } from \"graphql/language/ast\";\n\nexport enum GraphqlMethod {\n POST = \"POST\",\n GET = \"GET\",\n}\n\nexport type GraphQlExtraType = { headers: HeadersInit; extensions: Record<string, any> };\n\nexport type GraphQlEndpointType = string | DocumentNode;\n\nexport type FetchGraphqlAdapterOptionsType = Omit<RequestInit, \"method\" | \"headers\" | \"body\" | \"signal\"> & {\n timeout?: number;\n};\n\nexport type GraphqlAdapterType = Adapter<\n FetchGraphqlAdapterOptionsType,\n GraphqlMethod,\n number,\n GraphQlExtraType,\n QueryParamsType | string,\n undefined,\n GraphQlEndpointType,\n (endpoint: GraphQlEndpointType) => string\n>;\n\n// Extract\n\ntype GqlParamType = boolean | string | number | Record<string, any> | EmptyTypes;\n\nexport type ExtractGqlRouteParams<T extends string> = string extends T\n ? EmptyTypes\n : T extends `${string}query ${infer Parameters} {${infer QueryRest}`\n ? Parameters extends `${string}$${infer Param},${infer Rest}`\n ? GqlParameter<Param, Rest> &\n // eslint-disable-next-line @typescript-eslint/ban-types\n (ExtractGqlRouteParams<QueryRest> extends EmptyTypes ? {} : ExtractGqlRouteParams<QueryRest>)\n : Parameters extends `${string}$${infer Param})${infer Rest}`\n ? GqlParameter<Param, Rest> &\n // eslint-disable-next-line @typescript-eslint/ban-types\n (ExtractGqlRouteParams<QueryRest> extends EmptyTypes ? {} : ExtractGqlRouteParams<QueryRest>)\n : EmptyTypes\n : T extends `${string}$${infer Parameters},${infer Rest}`\n ? GqlParameter<Parameters, Rest>\n : T extends `${string}$${infer Parameters})${infer Rest}`\n ? GqlParameter<Parameters, Rest>\n : EmptyTypes;\n\nexport type GqlParameter<T extends string, Rest extends string> = T extends `${infer Param}:${string}!`\n ? { [k in Param]: GqlParamType } & (ExtractGqlRouteParams<Rest> extends EmptyTypes\n ? // eslint-disable-next-line @typescript-eslint/ban-types\n {}\n : ExtractGqlRouteParams<Rest>)\n : T extends `${infer Param}:${string}`\n ? { [k in Param]?: GqlParamType } & (ExtractGqlRouteParams<Rest> extends EmptyTypes\n ? // eslint-disable-next-line @typescript-eslint/ban-types\n {}\n : ExtractGqlRouteParams<Rest>)\n : EmptyTypes;\n","import type { RequestInstance } from \"@hyper-fetch/core\";\nimport { HttpMethods, stringifyQueryParams } from \"@hyper-fetch/core\";\nimport type { OperationDefinitionNode } from \"graphql\";\nimport { print, parse } from \"graphql\";\n\nimport type { GraphQlEndpointType } from \"adapter\";\n\nexport const getRequestValues = <R extends RequestInstance>(request: R) => {\n const isPostRequest = request.method === HttpMethods.POST;\n\n const query = request.endpoint;\n const variables = request.payload;\n\n const fullUrl = isPostRequest ? request.client.url : request.client.url + stringifyQueryParams({ query, variables });\n\n const payload = isPostRequest\n ? JSON.stringify({\n query,\n variables,\n })\n : null;\n\n return { fullUrl, payload };\n};\n\nexport const gqlEndpointMapper = <E extends GraphQlEndpointType>(endpoint: E): string => {\n if (typeof endpoint === \"string\") {\n return endpoint;\n }\n return print(endpoint);\n};\n\nexport const gqlEndpointNameMapper = (endpoint: string): string => {\n try {\n const ast = parse(endpoint);\n const operationDefinition = ast.definitions.find(\n (def): def is OperationDefinitionNode => def.kind === \"OperationDefinition\",\n );\n\n if (operationDefinition) {\n if (operationDefinition.name?.value) {\n return operationDefinition.name.value;\n }\n\n const selection = operationDefinition.selectionSet.selections[0];\n if (selection?.kind === \"Field\") {\n return selection.name.value;\n }\n if (selection?.kind === \"InlineFragment\" && selection.typeCondition) {\n return selection.typeCondition.name.value;\n }\n }\n } catch (error) {\n // We can ignore this error and just return some fallback\n }\n\n // Fallback for invalid gql queries or something else\n const clearedQuery = endpoint\n .replace(/(query|mutation|subscription)/, \"\")\n .replace(/[{}().,:]/g, \" \")\n .trim();\n const [name] = clearedQuery.split(\" \");\n\n return name || \"graphql-request\";\n};\n","import { Time } from \"@hyper-fetch/core\";\n\nimport type { GraphQlExtraType } from \"./adapter.types\";\n\nexport const defaultTimeout = Time.SEC * 5;\n\nexport const gqlExtra: GraphQlExtraType = {\n headers: {},\n extensions: {},\n};\n"],"mappings":";;;AAkBA,IAAa,sBACX,IAAI,QAQF;CACA,MAAM;CACN,eAAe,cAAc;CAC7B,c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C5C,QAAQ,cAAc,IAAI,CAC1B,MAAM,CACmB,MAAM,IAAI;AAEtC,QAAO,QAAQ;;;;AC3DjB,IAAa,iBAAiB,KAAK,MAAM;AAEzC,IAAa,WAA6B;CACxC,SAAS,EAAE;CACX,YAAY,EAAE;CACf"}
|
package/package.json
CHANGED
|
@@ -1,44 +1,29 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hyper-fetch/graphql",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "8.0.0",
|
|
4
4
|
"description": "Simple graphql adapter",
|
|
5
5
|
"author": "Maciej Pyrc <maciekpyrc@gmail.com>, Kacper Skawina <kacper.skawina@gmail.com>",
|
|
6
6
|
"homepage": "https://hyperfetch.bettertyped.com/",
|
|
7
7
|
"license": "Apache-2.0",
|
|
8
|
-
"main": "dist/index.
|
|
9
|
-
"module": "dist/index.
|
|
10
|
-
"client": "dist/browser/index.js",
|
|
11
|
-
"browser": "dist/browser/index.js",
|
|
12
|
-
"server": "dist/server/index.js",
|
|
13
|
-
"node": "dist/server/index.js",
|
|
8
|
+
"main": "dist/index.mjs",
|
|
9
|
+
"module": "dist/index.mjs",
|
|
14
10
|
"types": "dist/index.d.ts",
|
|
15
11
|
"source": "src/index.ts",
|
|
16
12
|
"exports": {
|
|
17
13
|
".": {
|
|
18
|
-
"types":
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
"browser": {
|
|
22
|
-
"require": "./dist/browser/index.js",
|
|
23
|
-
"import": "./dist/browser/index.js"
|
|
24
|
-
},
|
|
25
|
-
"default": {
|
|
26
|
-
"require": "./dist/index.js",
|
|
27
|
-
"import": "./dist/index.js"
|
|
28
|
-
}
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"require": "./dist/index.mjs",
|
|
16
|
+
"import": "./dist/index.mjs"
|
|
29
17
|
},
|
|
30
18
|
"./package.json": "./package.json"
|
|
31
19
|
},
|
|
32
20
|
"scripts": {
|
|
33
|
-
"
|
|
34
|
-
"test": "jest --watchAll --maxWorkers=3 --forceExit",
|
|
35
|
-
"test:pipeline": "jest --watchAll=false --maxWorkers=1 --no-cache --forceExit",
|
|
21
|
+
"test": "vitest run",
|
|
36
22
|
"start": "yarn build && npx run-when-changed --watch 'src/**/*.ts' --exec 'yarn build'",
|
|
37
|
-
"build": "
|
|
38
|
-
"postbuild": "yarn rollup -c ../../rollup.config.js",
|
|
23
|
+
"build": "vite build",
|
|
39
24
|
"lint": "eslint . --ext .js,.jsx,.tsx,.ts --fix",
|
|
40
25
|
"format": "prettier --write .",
|
|
41
|
-
"
|
|
26
|
+
"typecheck": "tsc --noEmit --emitDeclarationOnly false && tsc --noEmit -p __tests__/tsconfig.json",
|
|
42
27
|
"tests": "yarn lint-staged",
|
|
43
28
|
"release": "yarn semantic-release --extends ../../release.config.js -t 'hyper-fetch-graphql-v${version}'"
|
|
44
29
|
},
|
|
@@ -67,7 +52,7 @@
|
|
|
67
52
|
},
|
|
68
53
|
"lint-staged": {
|
|
69
54
|
"*.{js,jsx,ts,tsx}": [
|
|
70
|
-
"
|
|
55
|
+
"vitest related --run --passWithNoTests"
|
|
71
56
|
]
|
|
72
57
|
},
|
|
73
58
|
"keywords": [
|
package/project.json
ADDED
package/src/adapter/adapter.ts
CHANGED
|
@@ -1,11 +1,6 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
-
import { GraphqlAdapterType } from "./adapter.types";
|
|
1
|
+
import { getGqlAdapter } from "./http-adapter.fetch";
|
|
2
|
+
import type { GraphqlAdapterType } from "./adapter.types";
|
|
5
3
|
|
|
6
4
|
export const GraphqlAdapter = (): GraphqlAdapterType => {
|
|
7
|
-
|
|
8
|
-
return browser.getGqlAdapter();
|
|
9
|
-
}
|
|
10
|
-
return server.getGqlAdapter();
|
|
5
|
+
return getGqlAdapter();
|
|
11
6
|
};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { Adapter, EmptyTypes, QueryParamsType } from "@hyper-fetch/core";
|
|
2
|
-
import { DocumentNode } from "graphql/language/ast";
|
|
1
|
+
import type { Adapter, EmptyTypes, QueryParamsType } from "@hyper-fetch/core";
|
|
2
|
+
import type { DocumentNode } from "graphql/language/ast";
|
|
3
3
|
|
|
4
4
|
export enum GraphqlMethod {
|
|
5
5
|
POST = "POST",
|
|
@@ -10,8 +10,12 @@ export type GraphQlExtraType = { headers: HeadersInit; extensions: Record<string
|
|
|
10
10
|
|
|
11
11
|
export type GraphQlEndpointType = string | DocumentNode;
|
|
12
12
|
|
|
13
|
+
export type FetchGraphqlAdapterOptionsType = Omit<RequestInit, "method" | "headers" | "body" | "signal"> & {
|
|
14
|
+
timeout?: number;
|
|
15
|
+
};
|
|
16
|
+
|
|
13
17
|
export type GraphqlAdapterType = Adapter<
|
|
14
|
-
|
|
18
|
+
FetchGraphqlAdapterOptionsType,
|
|
15
19
|
GraphqlMethod,
|
|
16
20
|
number,
|
|
17
21
|
GraphQlExtraType,
|
|
@@ -1,7 +1,9 @@
|
|
|
1
|
-
import { RequestInstance
|
|
2
|
-
import {
|
|
1
|
+
import type { RequestInstance } from "@hyper-fetch/core";
|
|
2
|
+
import { HttpMethods, stringifyQueryParams } from "@hyper-fetch/core";
|
|
3
|
+
import type { OperationDefinitionNode } from "graphql";
|
|
4
|
+
import { print, parse } from "graphql";
|
|
3
5
|
|
|
4
|
-
import { GraphQlEndpointType } from "adapter";
|
|
6
|
+
import type { GraphQlEndpointType } from "adapter";
|
|
5
7
|
|
|
6
8
|
export const getRequestValues = <R extends RequestInstance>(request: R) => {
|
|
7
9
|
const isPostRequest = request.method === HttpMethods.POST;
|
|
@@ -44,6 +46,9 @@ export const gqlEndpointNameMapper = (endpoint: string): string => {
|
|
|
44
46
|
if (selection?.kind === "Field") {
|
|
45
47
|
return selection.name.value;
|
|
46
48
|
}
|
|
49
|
+
if (selection?.kind === "InlineFragment" && selection.typeCondition) {
|
|
50
|
+
return selection.typeCondition.name.value;
|
|
51
|
+
}
|
|
47
52
|
}
|
|
48
53
|
} catch (error) {
|
|
49
54
|
// We can ignore this error and just return some fallback
|