@nsxbet/react-relay-network-modern 1.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/LICENSE.md +10 -0
- package/README.md +324 -0
- package/dist/RRNLError.cjs +9 -0
- package/dist/RRNLError.d.cts +6 -0
- package/dist/RRNLError.d.ts +6 -0
- package/dist/RRNLError.js +9 -0
- package/dist/RelayNetworkLayer.cjs +54 -0
- package/dist/RelayNetworkLayer.d.cts +21 -0
- package/dist/RelayNetworkLayer.d.ts +21 -0
- package/dist/RelayNetworkLayer.js +54 -0
- package/dist/RelayRequest.cjs +90 -0
- package/dist/RelayRequest.d.cts +26 -0
- package/dist/RelayRequest.d.ts +26 -0
- package/dist/RelayRequest.js +90 -0
- package/dist/RelayRequestBatch.cjs +56 -0
- package/dist/RelayRequestBatch.d.cts +23 -0
- package/dist/RelayRequestBatch.d.ts +23 -0
- package/dist/RelayRequestBatch.js +56 -0
- package/dist/RelayResponse.cjs +56 -0
- package/dist/RelayResponse.d.cts +27 -0
- package/dist/RelayResponse.d.ts +27 -0
- package/dist/RelayResponse.js +56 -0
- package/dist/createRequestError.cjs +48 -0
- package/dist/createRequestError.d.cts +18 -0
- package/dist/createRequestError.d.ts +18 -0
- package/dist/createRequestError.js +46 -0
- package/dist/definition.d.cts +95 -0
- package/dist/definition.d.ts +95 -0
- package/dist/express-middleware/graphqlBatchHTTPWrapper.cjs +60 -0
- package/dist/express-middleware/graphqlBatchHTTPWrapper.d.cts +5 -0
- package/dist/express-middleware/graphqlBatchHTTPWrapper.d.ts +5 -0
- package/dist/express-middleware/graphqlBatchHTTPWrapper.js +60 -0
- package/dist/fetchWithMiddleware.cjs +43 -0
- package/dist/fetchWithMiddleware.js +43 -0
- package/dist/index.cjs +49 -0
- package/dist/index.d.cts +21 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.js +20 -0
- package/dist/middlewares/auth.cjs +47 -0
- package/dist/middlewares/auth.d.cts +18 -0
- package/dist/middlewares/auth.d.ts +18 -0
- package/dist/middlewares/auth.js +46 -0
- package/dist/middlewares/batch.cjs +136 -0
- package/dist/middlewares/batch.d.cts +34 -0
- package/dist/middlewares/batch.d.ts +34 -0
- package/dist/middlewares/batch.js +135 -0
- package/dist/middlewares/cache.cjs +43 -0
- package/dist/middlewares/cache.d.cts +17 -0
- package/dist/middlewares/cache.d.ts +17 -0
- package/dist/middlewares/cache.js +43 -0
- package/dist/middlewares/error.cjs +71 -0
- package/dist/middlewares/error.d.cts +11 -0
- package/dist/middlewares/error.d.ts +11 -0
- package/dist/middlewares/error.js +71 -0
- package/dist/middlewares/logger.cjs +43 -0
- package/dist/middlewares/logger.d.cts +9 -0
- package/dist/middlewares/logger.d.ts +9 -0
- package/dist/middlewares/logger.js +43 -0
- package/dist/middlewares/perf.cjs +13 -0
- package/dist/middlewares/perf.d.cts +9 -0
- package/dist/middlewares/perf.d.ts +9 -0
- package/dist/middlewares/perf.js +13 -0
- package/dist/middlewares/persistedQueries.cjs +33 -0
- package/dist/middlewares/persistedQueries.d.cts +9 -0
- package/dist/middlewares/persistedQueries.d.ts +9 -0
- package/dist/middlewares/persistedQueries.js +33 -0
- package/dist/middlewares/progress.cjs +35 -0
- package/dist/middlewares/progress.d.cts +10 -0
- package/dist/middlewares/progress.d.ts +10 -0
- package/dist/middlewares/progress.js +35 -0
- package/dist/middlewares/retry.cjs +146 -0
- package/dist/middlewares/retry.d.cts +34 -0
- package/dist/middlewares/retry.d.ts +34 -0
- package/dist/middlewares/retry.js +145 -0
- package/dist/middlewares/upload.cjs +32 -0
- package/dist/middlewares/upload.d.cts +6 -0
- package/dist/middlewares/upload.d.ts +6 -0
- package/dist/middlewares/upload.js +32 -0
- package/dist/middlewares/url.cjs +19 -0
- package/dist/middlewares/url.d.cts +19 -0
- package/dist/middlewares/url.d.ts +19 -0
- package/dist/middlewares/url.js +19 -0
- package/dist/utils.cjs +6 -0
- package/dist/utils.js +6 -0
- package/package.json +88 -0
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import RRNLError from "./RRNLError.js";
|
|
2
|
+
//#region src/RelayRequest.ts
|
|
3
|
+
function getFormDataInterface() {
|
|
4
|
+
return typeof window !== "undefined" && window.FormData || global && global.FormData;
|
|
5
|
+
}
|
|
6
|
+
var RelayRequest = class {
|
|
7
|
+
static lastGenId;
|
|
8
|
+
id;
|
|
9
|
+
fetchOpts;
|
|
10
|
+
operation;
|
|
11
|
+
variables;
|
|
12
|
+
cacheConfig;
|
|
13
|
+
uploadables;
|
|
14
|
+
controller;
|
|
15
|
+
constructor(operation, variables, cacheConfig, uploadables) {
|
|
16
|
+
this.operation = operation;
|
|
17
|
+
this.variables = variables;
|
|
18
|
+
this.cacheConfig = cacheConfig;
|
|
19
|
+
this.uploadables = uploadables;
|
|
20
|
+
this.id = this.operation.id || this.operation.name || this._generateID();
|
|
21
|
+
const fetchOpts = {
|
|
22
|
+
method: "POST",
|
|
23
|
+
headers: {},
|
|
24
|
+
body: this.prepareBody()
|
|
25
|
+
};
|
|
26
|
+
this.controller = typeof window !== "undefined" && window.AbortController ? new window.AbortController() : null;
|
|
27
|
+
if (this.controller) fetchOpts.signal = this.controller.signal;
|
|
28
|
+
this.fetchOpts = fetchOpts;
|
|
29
|
+
}
|
|
30
|
+
getBody() {
|
|
31
|
+
return this.fetchOpts.body;
|
|
32
|
+
}
|
|
33
|
+
prepareBody() {
|
|
34
|
+
const { uploadables } = this;
|
|
35
|
+
if (uploadables) {
|
|
36
|
+
const _FormData_ = getFormDataInterface();
|
|
37
|
+
if (!_FormData_) throw new RRNLError("Uploading files without `FormData` interface does not supported.");
|
|
38
|
+
const formData = new _FormData_();
|
|
39
|
+
formData.append("id", this.getID());
|
|
40
|
+
formData.append("query", this.getQueryString());
|
|
41
|
+
formData.append("variables", JSON.stringify(this.getVariables()));
|
|
42
|
+
Object.keys(uploadables).forEach((key) => {
|
|
43
|
+
if (Object.prototype.hasOwnProperty.call(uploadables, key)) formData.append(key, uploadables[key]);
|
|
44
|
+
});
|
|
45
|
+
return formData;
|
|
46
|
+
}
|
|
47
|
+
return JSON.stringify({
|
|
48
|
+
id: this.getID(),
|
|
49
|
+
query: this.getQueryString(),
|
|
50
|
+
variables: this.getVariables()
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
getID() {
|
|
54
|
+
return this.id;
|
|
55
|
+
}
|
|
56
|
+
_generateID() {
|
|
57
|
+
const ctor = this.constructor;
|
|
58
|
+
if (!ctor.lastGenId) ctor.lastGenId = 0;
|
|
59
|
+
ctor.lastGenId += 1;
|
|
60
|
+
return ctor.lastGenId.toString();
|
|
61
|
+
}
|
|
62
|
+
getQueryString() {
|
|
63
|
+
return this.operation.text || "";
|
|
64
|
+
}
|
|
65
|
+
getVariables() {
|
|
66
|
+
return this.variables;
|
|
67
|
+
}
|
|
68
|
+
isMutation() {
|
|
69
|
+
return this.operation.operationKind === "mutation";
|
|
70
|
+
}
|
|
71
|
+
isFormData() {
|
|
72
|
+
const _FormData_ = getFormDataInterface();
|
|
73
|
+
return !!_FormData_ && this.fetchOpts.body instanceof _FormData_;
|
|
74
|
+
}
|
|
75
|
+
cancel() {
|
|
76
|
+
if (this.controller) {
|
|
77
|
+
this.controller.abort();
|
|
78
|
+
return true;
|
|
79
|
+
}
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
clone() {
|
|
83
|
+
const newRequest = Object.assign(Object.create(Object.getPrototypeOf(this)), this);
|
|
84
|
+
newRequest.fetchOpts = { ...this.fetchOpts };
|
|
85
|
+
newRequest.fetchOpts.headers = { ...this.fetchOpts.headers };
|
|
86
|
+
return newRequest;
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
//#endregion
|
|
90
|
+
export { RelayRequest as default };
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
const require_RRNLError = require("./RRNLError.cjs");
|
|
2
|
+
//#region src/RelayRequestBatch.ts
|
|
3
|
+
var RelayRequestBatch = class {
|
|
4
|
+
fetchOpts;
|
|
5
|
+
requests;
|
|
6
|
+
constructor(requests) {
|
|
7
|
+
this.requests = requests;
|
|
8
|
+
this.fetchOpts = {
|
|
9
|
+
method: "POST",
|
|
10
|
+
headers: {},
|
|
11
|
+
body: this.prepareBody()
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
setFetchOption(name, value) {
|
|
15
|
+
this.fetchOpts[name] = value;
|
|
16
|
+
}
|
|
17
|
+
setFetchOptions(opts) {
|
|
18
|
+
this.fetchOpts = {
|
|
19
|
+
...this.fetchOpts,
|
|
20
|
+
...opts
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
getBody() {
|
|
24
|
+
if (!this.fetchOpts.body) this.fetchOpts.body = this.prepareBody();
|
|
25
|
+
return this.fetchOpts.body || "";
|
|
26
|
+
}
|
|
27
|
+
prepareBody() {
|
|
28
|
+
return `[${this.requests.map((r) => r.getBody()).join(",")}]`;
|
|
29
|
+
}
|
|
30
|
+
getIds() {
|
|
31
|
+
return this.requests.map((r) => r.getID());
|
|
32
|
+
}
|
|
33
|
+
getID() {
|
|
34
|
+
return `BATCH_REQUEST:${this.getIds().join(":")}`;
|
|
35
|
+
}
|
|
36
|
+
isMutation() {
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
isFormData() {
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
clone() {
|
|
43
|
+
const newRequest = Object.assign(Object.create(Object.getPrototypeOf(this)), this);
|
|
44
|
+
newRequest.fetchOpts = { ...this.fetchOpts };
|
|
45
|
+
newRequest.fetchOpts.headers = { ...this.fetchOpts.headers };
|
|
46
|
+
return newRequest;
|
|
47
|
+
}
|
|
48
|
+
getVariables() {
|
|
49
|
+
throw new require_RRNLError.default("Batch request does not have variables.");
|
|
50
|
+
}
|
|
51
|
+
getQueryString() {
|
|
52
|
+
return this.prepareBody();
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
//#endregion
|
|
56
|
+
exports.default = RelayRequestBatch;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { RelayRequest } from "./RelayRequest.cjs";
|
|
2
|
+
import { FetchOpts, Variables } from "./definition.cjs";
|
|
3
|
+
|
|
4
|
+
//#region src/RelayRequestBatch.d.ts
|
|
5
|
+
type Requests = RelayRequest[];
|
|
6
|
+
declare class RelayRequestBatch {
|
|
7
|
+
fetchOpts: Partial<FetchOpts>;
|
|
8
|
+
requests: Requests;
|
|
9
|
+
constructor(requests: Requests);
|
|
10
|
+
setFetchOption(name: string, value: unknown): void;
|
|
11
|
+
setFetchOptions(opts: Partial<FetchOpts>): void;
|
|
12
|
+
getBody(): string;
|
|
13
|
+
prepareBody(): string;
|
|
14
|
+
getIds(): string[];
|
|
15
|
+
getID(): string;
|
|
16
|
+
isMutation(): boolean;
|
|
17
|
+
isFormData(): boolean;
|
|
18
|
+
clone(): RelayRequestBatch;
|
|
19
|
+
getVariables(): Variables;
|
|
20
|
+
getQueryString(): string;
|
|
21
|
+
}
|
|
22
|
+
//#endregion
|
|
23
|
+
export { RelayRequestBatch };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { RelayRequest } from "./RelayRequest.js";
|
|
2
|
+
import { FetchOpts, Variables } from "./definition.js";
|
|
3
|
+
|
|
4
|
+
//#region src/RelayRequestBatch.d.ts
|
|
5
|
+
type Requests = RelayRequest[];
|
|
6
|
+
declare class RelayRequestBatch {
|
|
7
|
+
fetchOpts: Partial<FetchOpts>;
|
|
8
|
+
requests: Requests;
|
|
9
|
+
constructor(requests: Requests);
|
|
10
|
+
setFetchOption(name: string, value: unknown): void;
|
|
11
|
+
setFetchOptions(opts: Partial<FetchOpts>): void;
|
|
12
|
+
getBody(): string;
|
|
13
|
+
prepareBody(): string;
|
|
14
|
+
getIds(): string[];
|
|
15
|
+
getID(): string;
|
|
16
|
+
isMutation(): boolean;
|
|
17
|
+
isFormData(): boolean;
|
|
18
|
+
clone(): RelayRequestBatch;
|
|
19
|
+
getVariables(): Variables;
|
|
20
|
+
getQueryString(): string;
|
|
21
|
+
}
|
|
22
|
+
//#endregion
|
|
23
|
+
export { RelayRequestBatch };
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import RRNLError from "./RRNLError.js";
|
|
2
|
+
//#region src/RelayRequestBatch.ts
|
|
3
|
+
var RelayRequestBatch = class {
|
|
4
|
+
fetchOpts;
|
|
5
|
+
requests;
|
|
6
|
+
constructor(requests) {
|
|
7
|
+
this.requests = requests;
|
|
8
|
+
this.fetchOpts = {
|
|
9
|
+
method: "POST",
|
|
10
|
+
headers: {},
|
|
11
|
+
body: this.prepareBody()
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
setFetchOption(name, value) {
|
|
15
|
+
this.fetchOpts[name] = value;
|
|
16
|
+
}
|
|
17
|
+
setFetchOptions(opts) {
|
|
18
|
+
this.fetchOpts = {
|
|
19
|
+
...this.fetchOpts,
|
|
20
|
+
...opts
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
getBody() {
|
|
24
|
+
if (!this.fetchOpts.body) this.fetchOpts.body = this.prepareBody();
|
|
25
|
+
return this.fetchOpts.body || "";
|
|
26
|
+
}
|
|
27
|
+
prepareBody() {
|
|
28
|
+
return `[${this.requests.map((r) => r.getBody()).join(",")}]`;
|
|
29
|
+
}
|
|
30
|
+
getIds() {
|
|
31
|
+
return this.requests.map((r) => r.getID());
|
|
32
|
+
}
|
|
33
|
+
getID() {
|
|
34
|
+
return `BATCH_REQUEST:${this.getIds().join(":")}`;
|
|
35
|
+
}
|
|
36
|
+
isMutation() {
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
isFormData() {
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
clone() {
|
|
43
|
+
const newRequest = Object.assign(Object.create(Object.getPrototypeOf(this)), this);
|
|
44
|
+
newRequest.fetchOpts = { ...this.fetchOpts };
|
|
45
|
+
newRequest.fetchOpts.headers = { ...this.fetchOpts.headers };
|
|
46
|
+
return newRequest;
|
|
47
|
+
}
|
|
48
|
+
getVariables() {
|
|
49
|
+
throw new RRNLError("Batch request does not have variables.");
|
|
50
|
+
}
|
|
51
|
+
getQueryString() {
|
|
52
|
+
return this.prepareBody();
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
//#endregion
|
|
56
|
+
export { RelayRequestBatch as default };
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
//#region src/RelayResponse.ts
|
|
2
|
+
var RelayResponse = class RelayResponse {
|
|
3
|
+
_res;
|
|
4
|
+
data;
|
|
5
|
+
errors;
|
|
6
|
+
ok;
|
|
7
|
+
status;
|
|
8
|
+
statusText;
|
|
9
|
+
headers;
|
|
10
|
+
url;
|
|
11
|
+
text;
|
|
12
|
+
json;
|
|
13
|
+
static async createFromFetch(res) {
|
|
14
|
+
const r = new RelayResponse();
|
|
15
|
+
r._res = res;
|
|
16
|
+
r.ok = res.ok;
|
|
17
|
+
r.status = res.status;
|
|
18
|
+
r.url = res.url;
|
|
19
|
+
r.headers = res.headers;
|
|
20
|
+
if (res.status < 200 || res.status >= 300) r.text = await res.text();
|
|
21
|
+
else r.processJsonData(await res.json());
|
|
22
|
+
return r;
|
|
23
|
+
}
|
|
24
|
+
static async createFromGraphQL(res) {
|
|
25
|
+
const r = new RelayResponse();
|
|
26
|
+
r._res = res;
|
|
27
|
+
r.ok = true;
|
|
28
|
+
r.status = 200;
|
|
29
|
+
r.data = res.data;
|
|
30
|
+
r.errors = res.errors;
|
|
31
|
+
return r;
|
|
32
|
+
}
|
|
33
|
+
processJsonData(json) {
|
|
34
|
+
this.json = json;
|
|
35
|
+
if (json) {
|
|
36
|
+
const j = json;
|
|
37
|
+
if (j.data) this.data = j.data;
|
|
38
|
+
if (j.errors) this.errors = j.errors;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
clone() {
|
|
42
|
+
return Object.assign(Object.create(Object.getPrototypeOf(this)), this);
|
|
43
|
+
}
|
|
44
|
+
toString() {
|
|
45
|
+
return [
|
|
46
|
+
`Response:`,
|
|
47
|
+
` Url: ${this.url || ""}`,
|
|
48
|
+
` Status code: ${this.status || ""}`,
|
|
49
|
+
` Status text: ${this.statusText || ""}`,
|
|
50
|
+
` Response headers: ${JSON.stringify(this.headers) || ""}`,
|
|
51
|
+
` Response body: ${JSON.stringify(this.json) || ""}`
|
|
52
|
+
].join("\n");
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
//#endregion
|
|
56
|
+
exports.default = RelayResponse;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { FetchResponse, GraphQLResponseErrors, PayloadData } from "./definition.cjs";
|
|
2
|
+
|
|
3
|
+
//#region src/RelayResponse.d.ts
|
|
4
|
+
declare class RelayResponse {
|
|
5
|
+
_res: any;
|
|
6
|
+
data?: PayloadData | null;
|
|
7
|
+
errors?: GraphQLResponseErrors | null;
|
|
8
|
+
ok: any;
|
|
9
|
+
status: number;
|
|
10
|
+
statusText?: string | null;
|
|
11
|
+
headers?: {
|
|
12
|
+
[name: string]: string;
|
|
13
|
+
} | null;
|
|
14
|
+
url?: string | null;
|
|
15
|
+
text?: string | null;
|
|
16
|
+
json?: unknown;
|
|
17
|
+
static createFromFetch(res: FetchResponse): Promise<RelayResponse>;
|
|
18
|
+
static createFromGraphQL(res: {
|
|
19
|
+
errors?: any;
|
|
20
|
+
data?: any;
|
|
21
|
+
}): Promise<RelayResponse>;
|
|
22
|
+
processJsonData(json: unknown): void;
|
|
23
|
+
clone(): RelayResponse;
|
|
24
|
+
toString(): string;
|
|
25
|
+
}
|
|
26
|
+
//#endregion
|
|
27
|
+
export { RelayResponse };
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { FetchResponse, GraphQLResponseErrors, PayloadData } from "./definition.js";
|
|
2
|
+
|
|
3
|
+
//#region src/RelayResponse.d.ts
|
|
4
|
+
declare class RelayResponse {
|
|
5
|
+
_res: any;
|
|
6
|
+
data?: PayloadData | null;
|
|
7
|
+
errors?: GraphQLResponseErrors | null;
|
|
8
|
+
ok: any;
|
|
9
|
+
status: number;
|
|
10
|
+
statusText?: string | null;
|
|
11
|
+
headers?: {
|
|
12
|
+
[name: string]: string;
|
|
13
|
+
} | null;
|
|
14
|
+
url?: string | null;
|
|
15
|
+
text?: string | null;
|
|
16
|
+
json?: unknown;
|
|
17
|
+
static createFromFetch(res: FetchResponse): Promise<RelayResponse>;
|
|
18
|
+
static createFromGraphQL(res: {
|
|
19
|
+
errors?: any;
|
|
20
|
+
data?: any;
|
|
21
|
+
}): Promise<RelayResponse>;
|
|
22
|
+
processJsonData(json: unknown): void;
|
|
23
|
+
clone(): RelayResponse;
|
|
24
|
+
toString(): string;
|
|
25
|
+
}
|
|
26
|
+
//#endregion
|
|
27
|
+
export { RelayResponse };
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
//#region src/RelayResponse.ts
|
|
2
|
+
var RelayResponse = class RelayResponse {
|
|
3
|
+
_res;
|
|
4
|
+
data;
|
|
5
|
+
errors;
|
|
6
|
+
ok;
|
|
7
|
+
status;
|
|
8
|
+
statusText;
|
|
9
|
+
headers;
|
|
10
|
+
url;
|
|
11
|
+
text;
|
|
12
|
+
json;
|
|
13
|
+
static async createFromFetch(res) {
|
|
14
|
+
const r = new RelayResponse();
|
|
15
|
+
r._res = res;
|
|
16
|
+
r.ok = res.ok;
|
|
17
|
+
r.status = res.status;
|
|
18
|
+
r.url = res.url;
|
|
19
|
+
r.headers = res.headers;
|
|
20
|
+
if (res.status < 200 || res.status >= 300) r.text = await res.text();
|
|
21
|
+
else r.processJsonData(await res.json());
|
|
22
|
+
return r;
|
|
23
|
+
}
|
|
24
|
+
static async createFromGraphQL(res) {
|
|
25
|
+
const r = new RelayResponse();
|
|
26
|
+
r._res = res;
|
|
27
|
+
r.ok = true;
|
|
28
|
+
r.status = 200;
|
|
29
|
+
r.data = res.data;
|
|
30
|
+
r.errors = res.errors;
|
|
31
|
+
return r;
|
|
32
|
+
}
|
|
33
|
+
processJsonData(json) {
|
|
34
|
+
this.json = json;
|
|
35
|
+
if (json) {
|
|
36
|
+
const j = json;
|
|
37
|
+
if (j.data) this.data = j.data;
|
|
38
|
+
if (j.errors) this.errors = j.errors;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
clone() {
|
|
42
|
+
return Object.assign(Object.create(Object.getPrototypeOf(this)), this);
|
|
43
|
+
}
|
|
44
|
+
toString() {
|
|
45
|
+
return [
|
|
46
|
+
`Response:`,
|
|
47
|
+
` Url: ${this.url || ""}`,
|
|
48
|
+
` Status code: ${this.status || ""}`,
|
|
49
|
+
` Status text: ${this.statusText || ""}`,
|
|
50
|
+
` Response headers: ${JSON.stringify(this.headers) || ""}`,
|
|
51
|
+
` Response body: ${JSON.stringify(this.json) || ""}`
|
|
52
|
+
].join("\n");
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
//#endregion
|
|
56
|
+
export { RelayResponse as default };
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
const require_RRNLError = require("./RRNLError.cjs");
|
|
2
|
+
const require_RelayRequest = require("./RelayRequest.cjs");
|
|
3
|
+
//#region src/createRequestError.ts
|
|
4
|
+
var RRNLRequestError = class extends require_RRNLError.default {
|
|
5
|
+
req;
|
|
6
|
+
res;
|
|
7
|
+
constructor(msg) {
|
|
8
|
+
super(msg);
|
|
9
|
+
this.name = "RRNLRequestError";
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Formats an error response from GraphQL server request.
|
|
14
|
+
*/
|
|
15
|
+
function formatGraphQLErrors(request, errors) {
|
|
16
|
+
const CONTEXT_BEFORE = 20;
|
|
17
|
+
const CONTEXT_LENGTH = 60;
|
|
18
|
+
if (!request.getQueryString) return errors.join("\n");
|
|
19
|
+
let queryLines = [];
|
|
20
|
+
const queryString = request.getQueryString();
|
|
21
|
+
if (queryString) queryLines = queryString.split("\n");
|
|
22
|
+
return errors.map(({ locations, message }, ii) => {
|
|
23
|
+
const prefix = `${ii + 1}. `;
|
|
24
|
+
const indent = " ".repeat(prefix.length);
|
|
25
|
+
const locationMessage = locations && queryLines.length ? "\n" + locations.map(({ column, line }) => {
|
|
26
|
+
const queryLine = queryLines[line - 1];
|
|
27
|
+
const offset = Math.min(column - 1, CONTEXT_BEFORE);
|
|
28
|
+
return [queryLine.substr(column - 1 - offset, CONTEXT_LENGTH), `${" ".repeat(Math.max(offset, 0))}^^^`].map((messageLine) => indent + messageLine).join("\n");
|
|
29
|
+
}).join("\n") : "";
|
|
30
|
+
return prefix + message + locationMessage;
|
|
31
|
+
}).join("\n");
|
|
32
|
+
}
|
|
33
|
+
function createRequestError(req, res) {
|
|
34
|
+
let errorReason = "";
|
|
35
|
+
if (!res) errorReason = "Server return empty response.";
|
|
36
|
+
else if (res.errors) if (req instanceof require_RelayRequest.default) errorReason = formatGraphQLErrors(req, res.errors);
|
|
37
|
+
else errorReason = JSON.stringify(res.errors);
|
|
38
|
+
else if (!res.json) errorReason = (res.text ? res.text : `Server return empty response with Status Code: ${res.status}.`) + (res ? `\n\n${res.toString()}` : "");
|
|
39
|
+
else if (!res.data) errorReason = "Server return empty response.data.\n\n" + res.toString();
|
|
40
|
+
const error = new RRNLRequestError(`Relay request for \`${req.getID()}\` failed by the following reasons:\n\n${errorReason}`);
|
|
41
|
+
error.req = req;
|
|
42
|
+
error.res = res;
|
|
43
|
+
return error;
|
|
44
|
+
}
|
|
45
|
+
//#endregion
|
|
46
|
+
exports.RRNLRequestError = RRNLRequestError;
|
|
47
|
+
exports.createRequestError = createRequestError;
|
|
48
|
+
exports.formatGraphQLErrors = formatGraphQLErrors;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { RelayRequest } from "./RelayRequest.cjs";
|
|
2
|
+
import { RelayResponse } from "./RelayResponse.cjs";
|
|
3
|
+
import { GraphQLResponseErrors, RelayRequestAny } from "./definition.cjs";
|
|
4
|
+
import { RRNLError } from "./RRNLError.cjs";
|
|
5
|
+
|
|
6
|
+
//#region src/createRequestError.d.ts
|
|
7
|
+
declare class RRNLRequestError extends RRNLError {
|
|
8
|
+
req: RelayRequestAny;
|
|
9
|
+
res?: RelayResponse | null;
|
|
10
|
+
constructor(msg: string);
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Formats an error response from GraphQL server request.
|
|
14
|
+
*/
|
|
15
|
+
declare function formatGraphQLErrors(request: RelayRequest, errors: GraphQLResponseErrors): string;
|
|
16
|
+
declare function createRequestError(req: RelayRequestAny, res?: RelayResponse | null): RRNLRequestError;
|
|
17
|
+
//#endregion
|
|
18
|
+
export { RRNLRequestError, createRequestError, formatGraphQLErrors };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { RelayRequest } from "./RelayRequest.js";
|
|
2
|
+
import { RelayResponse } from "./RelayResponse.js";
|
|
3
|
+
import { GraphQLResponseErrors, RelayRequestAny } from "./definition.js";
|
|
4
|
+
import { RRNLError } from "./RRNLError.js";
|
|
5
|
+
|
|
6
|
+
//#region src/createRequestError.d.ts
|
|
7
|
+
declare class RRNLRequestError extends RRNLError {
|
|
8
|
+
req: RelayRequestAny;
|
|
9
|
+
res?: RelayResponse | null;
|
|
10
|
+
constructor(msg: string);
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Formats an error response from GraphQL server request.
|
|
14
|
+
*/
|
|
15
|
+
declare function formatGraphQLErrors(request: RelayRequest, errors: GraphQLResponseErrors): string;
|
|
16
|
+
declare function createRequestError(req: RelayRequestAny, res?: RelayResponse | null): RRNLRequestError;
|
|
17
|
+
//#endregion
|
|
18
|
+
export { RRNLRequestError, createRequestError, formatGraphQLErrors };
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import RRNLError from "./RRNLError.js";
|
|
2
|
+
import RelayRequest from "./RelayRequest.js";
|
|
3
|
+
//#region src/createRequestError.ts
|
|
4
|
+
var RRNLRequestError = class extends RRNLError {
|
|
5
|
+
req;
|
|
6
|
+
res;
|
|
7
|
+
constructor(msg) {
|
|
8
|
+
super(msg);
|
|
9
|
+
this.name = "RRNLRequestError";
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Formats an error response from GraphQL server request.
|
|
14
|
+
*/
|
|
15
|
+
function formatGraphQLErrors(request, errors) {
|
|
16
|
+
const CONTEXT_BEFORE = 20;
|
|
17
|
+
const CONTEXT_LENGTH = 60;
|
|
18
|
+
if (!request.getQueryString) return errors.join("\n");
|
|
19
|
+
let queryLines = [];
|
|
20
|
+
const queryString = request.getQueryString();
|
|
21
|
+
if (queryString) queryLines = queryString.split("\n");
|
|
22
|
+
return errors.map(({ locations, message }, ii) => {
|
|
23
|
+
const prefix = `${ii + 1}. `;
|
|
24
|
+
const indent = " ".repeat(prefix.length);
|
|
25
|
+
const locationMessage = locations && queryLines.length ? "\n" + locations.map(({ column, line }) => {
|
|
26
|
+
const queryLine = queryLines[line - 1];
|
|
27
|
+
const offset = Math.min(column - 1, CONTEXT_BEFORE);
|
|
28
|
+
return [queryLine.substr(column - 1 - offset, CONTEXT_LENGTH), `${" ".repeat(Math.max(offset, 0))}^^^`].map((messageLine) => indent + messageLine).join("\n");
|
|
29
|
+
}).join("\n") : "";
|
|
30
|
+
return prefix + message + locationMessage;
|
|
31
|
+
}).join("\n");
|
|
32
|
+
}
|
|
33
|
+
function createRequestError(req, res) {
|
|
34
|
+
let errorReason = "";
|
|
35
|
+
if (!res) errorReason = "Server return empty response.";
|
|
36
|
+
else if (res.errors) if (req instanceof RelayRequest) errorReason = formatGraphQLErrors(req, res.errors);
|
|
37
|
+
else errorReason = JSON.stringify(res.errors);
|
|
38
|
+
else if (!res.json) errorReason = (res.text ? res.text : `Server return empty response with Status Code: ${res.status}.`) + (res ? `\n\n${res.toString()}` : "");
|
|
39
|
+
else if (!res.data) errorReason = "Server return empty response.data.\n\n" + res.toString();
|
|
40
|
+
const error = new RRNLRequestError(`Relay request for \`${req.getID()}\` failed by the following reasons:\n\n${errorReason}`);
|
|
41
|
+
error.req = req;
|
|
42
|
+
error.res = res;
|
|
43
|
+
return error;
|
|
44
|
+
}
|
|
45
|
+
//#endregion
|
|
46
|
+
export { RRNLRequestError, createRequestError, formatGraphQLErrors };
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { RelayRequest } from "./RelayRequest.cjs";
|
|
2
|
+
import { RelayRequestBatch } from "./RelayRequestBatch.cjs";
|
|
3
|
+
import { RelayResponse } from "./RelayResponse.cjs";
|
|
4
|
+
|
|
5
|
+
//#region src/definition.d.ts
|
|
6
|
+
type RelayRequestAny = RelayRequest | RelayRequestBatch;
|
|
7
|
+
type MiddlewareNextFn = (req: RelayRequestAny) => Promise<RelayResponse>;
|
|
8
|
+
type Middleware = (next: MiddlewareNextFn) => MiddlewareNextFn;
|
|
9
|
+
type MiddlewareRawNextFn = (req: RelayRequestAny) => Promise<FetchResponse>;
|
|
10
|
+
interface MiddlewareRaw {
|
|
11
|
+
(next: MiddlewareRawNextFn): MiddlewareRawNextFn;
|
|
12
|
+
isRawMiddleware: true;
|
|
13
|
+
}
|
|
14
|
+
interface MiddlewareSync {
|
|
15
|
+
execute: (operation: ConcreteBatch, variables: Variables, cacheConfig: CacheConfig, uploadables?: UploadableMap | null) => ObservableFromValue<QueryPayload> | null | undefined;
|
|
16
|
+
}
|
|
17
|
+
interface FetchOpts {
|
|
18
|
+
url?: string;
|
|
19
|
+
method: 'POST' | 'GET';
|
|
20
|
+
headers: {
|
|
21
|
+
[name: string]: string;
|
|
22
|
+
};
|
|
23
|
+
body: string | FormData;
|
|
24
|
+
credentials?: 'same-origin' | 'include' | 'omit';
|
|
25
|
+
mode?: 'cors' | 'websocket' | 'navigate' | 'no-cors' | 'same-origin';
|
|
26
|
+
cache?: 'default' | 'no-store' | 'reload' | 'no-cache' | 'force-cache' | 'only-if-cached';
|
|
27
|
+
redirect?: 'follow' | 'error' | 'manual';
|
|
28
|
+
signal?: AbortSignal;
|
|
29
|
+
[name: string]: unknown;
|
|
30
|
+
}
|
|
31
|
+
type FetchResponse = Response;
|
|
32
|
+
type GraphQLResponseErrors = Array<{
|
|
33
|
+
message: string;
|
|
34
|
+
locations?: Array<{
|
|
35
|
+
column: number;
|
|
36
|
+
line: number;
|
|
37
|
+
}>;
|
|
38
|
+
stack?: Array<string>;
|
|
39
|
+
}>;
|
|
40
|
+
interface GraphQLResponse {
|
|
41
|
+
data?: any;
|
|
42
|
+
errors?: GraphQLResponseErrors;
|
|
43
|
+
}
|
|
44
|
+
type RNLExecuteFunction = (operation: ConcreteBatch, variables: Variables, cacheConfig: CacheConfig, uploadables?: UploadableMap | null) => RelayObservable<QueryPayload>;
|
|
45
|
+
interface Variables {
|
|
46
|
+
[name: string]: any;
|
|
47
|
+
}
|
|
48
|
+
interface ConcreteBatch {
|
|
49
|
+
kind: 'Batch';
|
|
50
|
+
fragment: any;
|
|
51
|
+
id: string | null;
|
|
52
|
+
metadata: {
|
|
53
|
+
[key: string]: unknown;
|
|
54
|
+
};
|
|
55
|
+
name: string;
|
|
56
|
+
query: any;
|
|
57
|
+
text: string | null;
|
|
58
|
+
operationKind: string;
|
|
59
|
+
}
|
|
60
|
+
interface CacheConfig {
|
|
61
|
+
force?: boolean | null;
|
|
62
|
+
poll?: number | null;
|
|
63
|
+
rerunParamExperimental?: any;
|
|
64
|
+
skipBatch?: boolean | null;
|
|
65
|
+
}
|
|
66
|
+
interface Disposable {
|
|
67
|
+
dispose(): void;
|
|
68
|
+
}
|
|
69
|
+
type Uploadable = File | Blob;
|
|
70
|
+
interface UploadableMap {
|
|
71
|
+
[key: string]: Uploadable;
|
|
72
|
+
}
|
|
73
|
+
interface PayloadData {
|
|
74
|
+
[key: string]: unknown;
|
|
75
|
+
}
|
|
76
|
+
type QueryPayload = {
|
|
77
|
+
data?: PayloadData | null;
|
|
78
|
+
errors?: Array<any>;
|
|
79
|
+
rerunVariables?: Variables;
|
|
80
|
+
} | RelayResponse;
|
|
81
|
+
type UnsubscribeFunction = () => void;
|
|
82
|
+
interface Sink<T> {
|
|
83
|
+
next: (value: T) => void;
|
|
84
|
+
complete: () => void;
|
|
85
|
+
error: (value: T) => void;
|
|
86
|
+
}
|
|
87
|
+
interface RelayObservable<T> {
|
|
88
|
+
subscribe: (sink: Sink<T>) => UnsubscribeFunction;
|
|
89
|
+
}
|
|
90
|
+
type ObservableFromValue<T> = RelayObservable<T> | Promise<T> | T;
|
|
91
|
+
type FetchFunction = (operation: ConcreteBatch, variables: Variables, cacheConfig: CacheConfig, uploadables?: UploadableMap | null) => ObservableFromValue<QueryPayload>;
|
|
92
|
+
type FetchHookFunction = (operation: ConcreteBatch, variables: Variables, cacheConfig: CacheConfig, uploadables?: UploadableMap | null) => void | ObservableFromValue<QueryPayload>;
|
|
93
|
+
type SubscribeFunction = (operation: ConcreteBatch, variables: Variables, cacheConfig: CacheConfig, observer: any) => RelayObservable<QueryPayload> | Disposable;
|
|
94
|
+
//#endregion
|
|
95
|
+
export { CacheConfig, ConcreteBatch, Disposable, FetchFunction, FetchHookFunction, FetchOpts, FetchResponse, GraphQLResponse, GraphQLResponseErrors, Middleware, MiddlewareNextFn, MiddlewareRaw, MiddlewareRawNextFn, MiddlewareSync, ObservableFromValue, PayloadData, QueryPayload, RNLExecuteFunction, RelayObservable, RelayRequestAny, SubscribeFunction, Uploadable, UploadableMap, Variables };
|