@superbia/untrue 1.4.0 → 2.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/dist/Api.d.ts +14 -0
- package/dist/Api.js +109 -0
- package/dist/DocumentContext.d.ts +15 -0
- package/dist/DocumentContext.js +45 -0
- package/dist/RequestContext.d.ts +24 -0
- package/dist/RequestContext.js +39 -0
- package/dist/SuperbiaContext.d.ts +19 -0
- package/dist/SuperbiaContext.js +39 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +8 -0
- package/package.json +12 -3
- package/index.js +0 -4
- package/src/DocumentContext.js +0 -58
- package/src/RequestContext.js +0 -155
- package/src/SuperbiaContext.js +0 -48
- package/src/SuperbiaUntrue.js +0 -55
package/dist/Api.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Client, EndpointRecord, Response, ResponseResult } from "@superbia/client";
|
|
2
|
+
import { DocumentSchemaRecord } from "./SuperbiaContext";
|
|
3
|
+
import { DocumentContext, Documents } from "./DocumentContext";
|
|
4
|
+
import { RequestContext, Request } from "./RequestContext";
|
|
5
|
+
export default class Api<K extends DocumentSchemaRecord, M extends EndpointRecord, N extends EndpointRecord = {}, O extends string = "id"> {
|
|
6
|
+
readonly client: Client<M, N>;
|
|
7
|
+
readonly documents: DocumentContext<K, M>;
|
|
8
|
+
readonly requests: RequestContext<M, O>;
|
|
9
|
+
constructor(client: Client<M, N>, idKey?: string);
|
|
10
|
+
useDocuments<W>(selector: (documents: Documents<K>) => W): W;
|
|
11
|
+
useRequest<Y extends ResponseResult, W, X extends any[] = any[]>(key: string, selector: (request: Request<Y, O>) => W, requester?: (...args: X) => Promise<Response<Y>>): [W, (...args: X) => Promise<void>];
|
|
12
|
+
useLoad<Y extends ResponseResult, W, X extends any[]>(key: string, selector: (request: Request<Y, O>) => W, loader: (...args: X) => Promise<Response<Y>>): [W, (...args: X) => Promise<void>];
|
|
13
|
+
useRequestKey(params?: any[]): string;
|
|
14
|
+
}
|
package/dist/Api.js
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const untrue_1 = require("untrue");
|
|
4
|
+
const uuid_1 = require("uuid");
|
|
5
|
+
const DocumentContext_1 = require("./DocumentContext");
|
|
6
|
+
const RequestContext_1 = require("./RequestContext");
|
|
7
|
+
class Api {
|
|
8
|
+
constructor(client, idKey = "id") {
|
|
9
|
+
this.client = client;
|
|
10
|
+
this.documents = new DocumentContext_1.DocumentContext(client, idKey);
|
|
11
|
+
this.requests = new RequestContext_1.RequestContext(client, idKey);
|
|
12
|
+
}
|
|
13
|
+
useDocuments(selector) {
|
|
14
|
+
return untrue_1.Hook.useContext(this.documents, () => selector(this.documents.data));
|
|
15
|
+
}
|
|
16
|
+
useRequest(key, selector, requester) {
|
|
17
|
+
const value = untrue_1.Hook.useContext(this.requests, () => {
|
|
18
|
+
var _a;
|
|
19
|
+
let request = ((_a = this.requests.data[key]) !== null && _a !== void 0 ? _a : null);
|
|
20
|
+
if (request === null) {
|
|
21
|
+
request = {
|
|
22
|
+
loading: false,
|
|
23
|
+
done: false,
|
|
24
|
+
result: null,
|
|
25
|
+
error: null,
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
return selector(request);
|
|
29
|
+
});
|
|
30
|
+
const request = async (...args) => {
|
|
31
|
+
if (requester === undefined) {
|
|
32
|
+
throw new Error("Requester not defined in useRequest.");
|
|
33
|
+
}
|
|
34
|
+
this.requests.data[key] = {
|
|
35
|
+
loading: true,
|
|
36
|
+
done: false,
|
|
37
|
+
result: null,
|
|
38
|
+
error: null,
|
|
39
|
+
};
|
|
40
|
+
this.requests.update();
|
|
41
|
+
try {
|
|
42
|
+
const response = await requester(...args);
|
|
43
|
+
const result = response.result();
|
|
44
|
+
const parsedResult = this.requests.parseResult(result);
|
|
45
|
+
this.requests.data[key] = {
|
|
46
|
+
loading: false,
|
|
47
|
+
done: true,
|
|
48
|
+
result: parsedResult,
|
|
49
|
+
error: null,
|
|
50
|
+
};
|
|
51
|
+
this.requests.update();
|
|
52
|
+
}
|
|
53
|
+
catch (error) {
|
|
54
|
+
this.requests.data[key] = {
|
|
55
|
+
loading: false,
|
|
56
|
+
done: false,
|
|
57
|
+
result: null,
|
|
58
|
+
error,
|
|
59
|
+
};
|
|
60
|
+
this.requests.update();
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
return [value, request];
|
|
64
|
+
}
|
|
65
|
+
useLoad(key, selector, loader) {
|
|
66
|
+
const value = untrue_1.Hook.useContext(this.requests, () => {
|
|
67
|
+
const request = this.requests.data[key];
|
|
68
|
+
return selector(request);
|
|
69
|
+
});
|
|
70
|
+
const load = async (...args) => {
|
|
71
|
+
const request = this.requests.data[key];
|
|
72
|
+
const tmpResult = request.result;
|
|
73
|
+
let tmpPagination;
|
|
74
|
+
let endpointName;
|
|
75
|
+
this.client.once("request", (endpoints) => {
|
|
76
|
+
endpointName = Object.keys(endpoints)[0];
|
|
77
|
+
tmpPagination = tmpResult[endpointName];
|
|
78
|
+
tmpPagination.loading = true;
|
|
79
|
+
tmpPagination.error = null;
|
|
80
|
+
this.requests.update();
|
|
81
|
+
});
|
|
82
|
+
endpointName = endpointName;
|
|
83
|
+
tmpPagination = tmpPagination;
|
|
84
|
+
try {
|
|
85
|
+
const response = await loader(...args);
|
|
86
|
+
const result = response.result();
|
|
87
|
+
const parsedResult = this.requests.parseResult(result);
|
|
88
|
+
const endpointResult = parsedResult[endpointName];
|
|
89
|
+
tmpPagination.loading = false;
|
|
90
|
+
tmpPagination.error = null;
|
|
91
|
+
tmpPagination.result.hasNextPage = endpointResult.result.hasNextPage;
|
|
92
|
+
tmpPagination.result.nextPageCursor =
|
|
93
|
+
endpointResult.result.nextPageCursor;
|
|
94
|
+
tmpPagination.result.nodes.push(...endpointResult.result.nodes);
|
|
95
|
+
this.requests.update();
|
|
96
|
+
}
|
|
97
|
+
catch (error) {
|
|
98
|
+
tmpPagination.loading = false;
|
|
99
|
+
tmpPagination.error = error;
|
|
100
|
+
this.requests.update();
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
return [value, load];
|
|
104
|
+
}
|
|
105
|
+
useRequestKey(params) {
|
|
106
|
+
return untrue_1.Hook.useMemo(() => (0, uuid_1.v4)(), params);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
exports.default = Api;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Client, Result, EndpointRecord } from "@superbia/client";
|
|
2
|
+
import { ParsedResult, SuperbiaContext, DocumentSchema, DocumentSchemaRecord } from "./SuperbiaContext";
|
|
3
|
+
export type Document<T extends DocumentSchema> = {
|
|
4
|
+
[K in keyof T]: ParsedResult<T[K]>;
|
|
5
|
+
};
|
|
6
|
+
export type Documents<T extends DocumentSchemaRecord> = {
|
|
7
|
+
[K in keyof T]: Record<string, Result<Document<T[K]>>>;
|
|
8
|
+
};
|
|
9
|
+
export declare class DocumentContext<K extends DocumentSchemaRecord, M extends EndpointRecord> extends SuperbiaContext<M> {
|
|
10
|
+
data: Documents<K>;
|
|
11
|
+
constructor(client: Client<M, any>, idKey: string);
|
|
12
|
+
hydrate(data: any): void;
|
|
13
|
+
persist(): any;
|
|
14
|
+
private handleResult;
|
|
15
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DocumentContext = void 0;
|
|
4
|
+
const SuperbiaContext_1 = require("./SuperbiaContext");
|
|
5
|
+
class DocumentContext extends SuperbiaContext_1.SuperbiaContext {
|
|
6
|
+
constructor(client, idKey) {
|
|
7
|
+
super(client, idKey);
|
|
8
|
+
this.data = {};
|
|
9
|
+
const listener = (endpoints, emitter) => {
|
|
10
|
+
emitter.on("result", (result) => {
|
|
11
|
+
this.handleResult(result);
|
|
12
|
+
});
|
|
13
|
+
};
|
|
14
|
+
client.on("request", listener);
|
|
15
|
+
client.on("subscribe", listener);
|
|
16
|
+
}
|
|
17
|
+
hydrate(data) {
|
|
18
|
+
this.data = data;
|
|
19
|
+
}
|
|
20
|
+
persist() {
|
|
21
|
+
return this.data;
|
|
22
|
+
}
|
|
23
|
+
handleResult(result) {
|
|
24
|
+
const data = {};
|
|
25
|
+
for (const tmpResult of Object.values(result)) {
|
|
26
|
+
this.parseResultValue(tmpResult, data);
|
|
27
|
+
}
|
|
28
|
+
const types = Object.keys(data);
|
|
29
|
+
if (types.length === 0) {
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
const documents = this.data;
|
|
33
|
+
for (const type of types) {
|
|
34
|
+
if (!(type in documents)) {
|
|
35
|
+
documents[type] = {};
|
|
36
|
+
}
|
|
37
|
+
const tmpDocuments = data[type];
|
|
38
|
+
for (const id in tmpDocuments) {
|
|
39
|
+
documents[type][id] = tmpDocuments[id];
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
this.update();
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
exports.DocumentContext = DocumentContext;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { Result, Pagination, EndpointRecord, ResponseResult } from "@superbia/client";
|
|
2
|
+
import { ParsedResult, SuperbiaContext } from "./SuperbiaContext";
|
|
3
|
+
export interface PaginationResult<T, O extends string> {
|
|
4
|
+
loading: boolean;
|
|
5
|
+
result: ParsedResult<T, O>;
|
|
6
|
+
error: Error | null;
|
|
7
|
+
}
|
|
8
|
+
export type Request<T extends ResponseResult, O extends string> = {
|
|
9
|
+
loading: boolean;
|
|
10
|
+
done: boolean;
|
|
11
|
+
result: {
|
|
12
|
+
[K in keyof T]: T[K] extends Pagination<any> ? PaginationResult<T[K], O> : ParsedResult<T[K], O>;
|
|
13
|
+
} | null;
|
|
14
|
+
error: Error | null;
|
|
15
|
+
};
|
|
16
|
+
export type Requests<T extends EndpointRecord, O extends string> = Record<string, Request<{
|
|
17
|
+
[K in keyof T]: Result<T[K]["result"]>;
|
|
18
|
+
}, O>>;
|
|
19
|
+
export declare class RequestContext<M extends EndpointRecord, O extends string> extends SuperbiaContext<M> {
|
|
20
|
+
data: Requests<M, O>;
|
|
21
|
+
hydrate(data: any): void;
|
|
22
|
+
persist(): any;
|
|
23
|
+
parseResult(result: ResponseResult): ResponseResult;
|
|
24
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RequestContext = void 0;
|
|
4
|
+
const SuperbiaContext_1 = require("./SuperbiaContext");
|
|
5
|
+
class RequestContext extends SuperbiaContext_1.SuperbiaContext {
|
|
6
|
+
constructor() {
|
|
7
|
+
super(...arguments);
|
|
8
|
+
this.data = {};
|
|
9
|
+
}
|
|
10
|
+
hydrate(data) {
|
|
11
|
+
this.data = data;
|
|
12
|
+
}
|
|
13
|
+
persist() {
|
|
14
|
+
return this.data;
|
|
15
|
+
}
|
|
16
|
+
parseResult(result) {
|
|
17
|
+
const tmpResult = {};
|
|
18
|
+
for (const key in result) {
|
|
19
|
+
const endpointResult = result[key];
|
|
20
|
+
let tmpEndpointResult;
|
|
21
|
+
if (endpointResult !== null &&
|
|
22
|
+
typeof endpointResult === "object" &&
|
|
23
|
+
this.typenameKey in endpointResult &&
|
|
24
|
+
endpointResult[this.typenameKey].endsWith("Pagination")) {
|
|
25
|
+
tmpEndpointResult = {
|
|
26
|
+
loading: false,
|
|
27
|
+
result: this.parseResultValue(endpointResult),
|
|
28
|
+
error: null,
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
tmpEndpointResult = this.parseResultValue(endpointResult);
|
|
33
|
+
}
|
|
34
|
+
tmpResult[key] = tmpEndpointResult;
|
|
35
|
+
}
|
|
36
|
+
return tmpResult;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
exports.RequestContext = RequestContext;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Context } from "untrue";
|
|
2
|
+
import { Client, EndpointRecord } from "@superbia/client";
|
|
3
|
+
type IdObject<U extends string> = {
|
|
4
|
+
[K in U]: string;
|
|
5
|
+
};
|
|
6
|
+
export type ParsedResult<T, U extends string = "id"> = T extends IdObject<U> ? string : T extends IdObject<U> | null ? string | null : T extends IdObject<U>[] ? string[] : T extends (IdObject<U> | null)[] ? (string | null)[] : T extends null ? null : T extends object ? {
|
|
7
|
+
[K in keyof T]: ParsedResult<T[K], U>;
|
|
8
|
+
} : T;
|
|
9
|
+
export type DocumentSchema = Record<string, any>;
|
|
10
|
+
export type DocumentSchemaRecord = Record<string, DocumentSchema>;
|
|
11
|
+
export type DocumentData = Record<string, Record<string, DocumentSchema>>;
|
|
12
|
+
export declare class SuperbiaContext<M extends EndpointRecord> extends Context {
|
|
13
|
+
protected client: Client<M, any>;
|
|
14
|
+
protected idKey: string;
|
|
15
|
+
protected typenameKey: string;
|
|
16
|
+
constructor(client: Client<M, any>, idKey: string);
|
|
17
|
+
parseResultValue(result: any, data?: DocumentData): any;
|
|
18
|
+
}
|
|
19
|
+
export {};
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SuperbiaContext = void 0;
|
|
4
|
+
const untrue_1 = require("untrue");
|
|
5
|
+
class SuperbiaContext extends untrue_1.Context {
|
|
6
|
+
constructor(client, idKey) {
|
|
7
|
+
super();
|
|
8
|
+
this.client = client;
|
|
9
|
+
this.idKey = idKey;
|
|
10
|
+
this.typenameKey = "_typename";
|
|
11
|
+
}
|
|
12
|
+
parseResultValue(result, data = {}) {
|
|
13
|
+
if (result === null) {
|
|
14
|
+
return null;
|
|
15
|
+
}
|
|
16
|
+
if (Array.isArray(result)) {
|
|
17
|
+
return result.map((element) => this.parseResultValue(element, data));
|
|
18
|
+
}
|
|
19
|
+
if (typeof result === "object") {
|
|
20
|
+
const newResult = {};
|
|
21
|
+
for (const key in result) {
|
|
22
|
+
newResult[key] = this.parseResultValue(result[key], data);
|
|
23
|
+
}
|
|
24
|
+
const isDocument = this.idKey in result && this.typenameKey in result;
|
|
25
|
+
if (!isDocument) {
|
|
26
|
+
return newResult;
|
|
27
|
+
}
|
|
28
|
+
const id = result[this.idKey];
|
|
29
|
+
const typename = result[this.typenameKey];
|
|
30
|
+
if (!(typename in data)) {
|
|
31
|
+
data[typename] = {};
|
|
32
|
+
}
|
|
33
|
+
data[typename][id] = newResult;
|
|
34
|
+
return result[this.idKey];
|
|
35
|
+
}
|
|
36
|
+
return result;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
exports.SuperbiaContext = SuperbiaContext;
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
var Api_1 = require("./Api");
|
|
8
|
+
Object.defineProperty(exports, "default", { enumerable: true, get: function () { return __importDefault(Api_1).default; } });
|
package/package.json
CHANGED
|
@@ -1,21 +1,30 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@superbia/untrue",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "Integrate Superbia and Untrue.",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
7
7
|
"url": "https://github.com/iconshot/superbia-untrue.git"
|
|
8
8
|
},
|
|
9
|
-
"main": "index.js",
|
|
9
|
+
"main": "dist/index.js",
|
|
10
|
+
"scripts": {
|
|
11
|
+
"clean": "rm -rf dist",
|
|
12
|
+
"build": "npm run clean && tsc",
|
|
13
|
+
"prepare": "npm run build"
|
|
14
|
+
},
|
|
10
15
|
"keywords": [
|
|
11
16
|
"superbia",
|
|
12
17
|
"untrue"
|
|
13
18
|
],
|
|
14
19
|
"license": "MIT",
|
|
15
20
|
"peerDependencies": {
|
|
16
|
-
"untrue": "^5.
|
|
21
|
+
"untrue": "^5.8.0",
|
|
22
|
+
"@superbia/client": "^2.0.8"
|
|
17
23
|
},
|
|
18
24
|
"dependencies": {
|
|
19
25
|
"uuid": "^10.0.0"
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"typescript": "^5.6.3"
|
|
20
29
|
}
|
|
21
30
|
}
|
package/index.js
DELETED
package/src/DocumentContext.js
DELETED
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
import SuperbiaContext from "./SuperbiaContext";
|
|
2
|
-
|
|
3
|
-
export class DocumentContext extends SuperbiaContext {
|
|
4
|
-
constructor(client, id) {
|
|
5
|
-
super(id);
|
|
6
|
-
|
|
7
|
-
this.documents = {};
|
|
8
|
-
|
|
9
|
-
const listener = (endpoints, emitter) => {
|
|
10
|
-
emitter.on("data", (data) => this.data(data));
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
client.on("request", listener);
|
|
14
|
-
client.on("subscribe", listener);
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
// default persistence
|
|
18
|
-
|
|
19
|
-
hydrate(documents) {
|
|
20
|
-
this.documents = documents;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
persist() {
|
|
24
|
-
return this.documents;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
getDocuments() {
|
|
28
|
-
return this.documents;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
data(data) {
|
|
32
|
-
const newData = {};
|
|
33
|
-
|
|
34
|
-
Object.values(data).forEach((result) => {
|
|
35
|
-
this.parseResult(result, newData);
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
const types = Object.keys(newData);
|
|
39
|
-
|
|
40
|
-
if (types.length === 0) {
|
|
41
|
-
return;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
for (const type of types) {
|
|
45
|
-
if (!(type in this.documents)) {
|
|
46
|
-
this.documents[type] = {};
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
const newDocuments = newData[type];
|
|
50
|
-
|
|
51
|
-
for (const id in newDocuments) {
|
|
52
|
-
this.documents[type][id] = newDocuments[id];
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
this.update();
|
|
57
|
-
}
|
|
58
|
-
}
|
package/src/RequestContext.js
DELETED
|
@@ -1,155 +0,0 @@
|
|
|
1
|
-
import SuperbiaContext from "./SuperbiaContext";
|
|
2
|
-
|
|
3
|
-
export class RequestContext extends SuperbiaContext {
|
|
4
|
-
constructor(client, id) {
|
|
5
|
-
super(id);
|
|
6
|
-
|
|
7
|
-
this.requests = {};
|
|
8
|
-
|
|
9
|
-
this.client = client;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
// default persistence
|
|
13
|
-
|
|
14
|
-
hydrate(requests) {
|
|
15
|
-
this.requests = requests;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
persist() {
|
|
19
|
-
return this.requests;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
getRequests() {
|
|
23
|
-
return this.requests;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
intercept() {
|
|
27
|
-
return {};
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
parseRequestResult(result) {
|
|
31
|
-
if (
|
|
32
|
-
result !== null &&
|
|
33
|
-
typeof result === "object" &&
|
|
34
|
-
this.keys.typename in result &&
|
|
35
|
-
result[this.keys.typename].endsWith("Pagination")
|
|
36
|
-
) {
|
|
37
|
-
return { loading: false, error: null, data: this.parseResult(result) };
|
|
38
|
-
} else {
|
|
39
|
-
return this.parseResult(result);
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
async request(key, endpoints, payload = null) {
|
|
44
|
-
key = key !== null && key !== undefined ? key : Date.now().toString();
|
|
45
|
-
|
|
46
|
-
this.requests[key] = {
|
|
47
|
-
loading: true,
|
|
48
|
-
done: false,
|
|
49
|
-
error: null,
|
|
50
|
-
data: null,
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
const interceptors = this.intercept();
|
|
54
|
-
|
|
55
|
-
const endpointKeys = Object.keys(endpoints);
|
|
56
|
-
|
|
57
|
-
for (const endpointKey of endpointKeys) {
|
|
58
|
-
if (endpointKey in interceptors && "load" in interceptors[endpointKey]) {
|
|
59
|
-
interceptors[endpointKey].load(key, endpoints, payload);
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
this.update();
|
|
64
|
-
|
|
65
|
-
try {
|
|
66
|
-
const response = await this.client.request(endpoints);
|
|
67
|
-
|
|
68
|
-
const data = response.data();
|
|
69
|
-
|
|
70
|
-
const newData = {};
|
|
71
|
-
|
|
72
|
-
for (const key in data) {
|
|
73
|
-
newData[key] = this.parseRequestResult(data[key]);
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
this.requests[key] = {
|
|
77
|
-
loading: false,
|
|
78
|
-
done: true,
|
|
79
|
-
error: null,
|
|
80
|
-
data: newData,
|
|
81
|
-
};
|
|
82
|
-
|
|
83
|
-
for (const endpointKey of endpointKeys) {
|
|
84
|
-
if (
|
|
85
|
-
endpointKey in interceptors &&
|
|
86
|
-
"data" in interceptors[endpointKey]
|
|
87
|
-
) {
|
|
88
|
-
interceptors[endpointKey].data(key, endpoints, payload, data);
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
this.update();
|
|
93
|
-
} catch (error) {
|
|
94
|
-
this.requests[key] = { loading: false, done: false, error, data: null };
|
|
95
|
-
|
|
96
|
-
for (const endpointKey of endpointKeys) {
|
|
97
|
-
if (
|
|
98
|
-
endpointKey in interceptors &&
|
|
99
|
-
"error" in interceptors[endpointKey]
|
|
100
|
-
) {
|
|
101
|
-
interceptors[endpointKey].error(key, endpoints, payload, error);
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
this.update();
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
async load(key, endpoints, payload = null) {
|
|
110
|
-
const interceptors = this.intercept();
|
|
111
|
-
|
|
112
|
-
const endpointKey = Object.keys(endpoints)[0];
|
|
113
|
-
|
|
114
|
-
const result = this.requests[key].data[endpointKey];
|
|
115
|
-
|
|
116
|
-
result.loading = true;
|
|
117
|
-
result.error = null;
|
|
118
|
-
|
|
119
|
-
if (endpointKey in interceptors && "load" in interceptors[endpointKey]) {
|
|
120
|
-
interceptors[endpointKey].load(key, endpoints, payload);
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
this.update();
|
|
124
|
-
|
|
125
|
-
try {
|
|
126
|
-
const response = await this.client.request(endpoints);
|
|
127
|
-
|
|
128
|
-
const data = response.data();
|
|
129
|
-
|
|
130
|
-
const endpointResult = this.parseResult(data[endpointKey]);
|
|
131
|
-
|
|
132
|
-
result.loading = false;
|
|
133
|
-
|
|
134
|
-
result.data = {
|
|
135
|
-
...endpointResult,
|
|
136
|
-
nodes: [...result.data.nodes, ...endpointResult.nodes],
|
|
137
|
-
};
|
|
138
|
-
|
|
139
|
-
if (endpointKey in interceptors && "data" in interceptors[endpointKey]) {
|
|
140
|
-
interceptors[endpointKey].data(key, endpoints, payload, data);
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
this.update();
|
|
144
|
-
} catch (error) {
|
|
145
|
-
result.loading = false;
|
|
146
|
-
result.error = error;
|
|
147
|
-
|
|
148
|
-
if (endpointKey in interceptors && "error" in interceptors[endpointKey]) {
|
|
149
|
-
interceptors[endpointKey].error(key, endpoints, payload, error);
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
this.update();
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
}
|
package/src/SuperbiaContext.js
DELETED
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
import { Context } from "untrue";
|
|
2
|
-
|
|
3
|
-
class SuperbiaContext extends Context {
|
|
4
|
-
constructor(id = "id") {
|
|
5
|
-
super();
|
|
6
|
-
|
|
7
|
-
this.keys = { id, typename: "_typename" };
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
parseResult(result, data = {}) {
|
|
11
|
-
if (result === null) {
|
|
12
|
-
return null;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
if (Array.isArray(result)) {
|
|
16
|
-
return result.map((element) => this.parseResult(element, data));
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
if (typeof result === "object") {
|
|
20
|
-
const newResult = {};
|
|
21
|
-
|
|
22
|
-
for (const key in result) {
|
|
23
|
-
newResult[key] = this.parseResult(result[key], data);
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
const isDocument = this.keys.id in result && this.keys.typename in result;
|
|
27
|
-
|
|
28
|
-
if (!isDocument) {
|
|
29
|
-
return newResult;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
const id = result[this.keys.id];
|
|
33
|
-
const typename = result[this.keys.typename];
|
|
34
|
-
|
|
35
|
-
if (!(typename in data)) {
|
|
36
|
-
data[typename] = {};
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
data[typename][id] = newResult;
|
|
40
|
-
|
|
41
|
-
return result[this.keys.id];
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
return result;
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
export default SuperbiaContext;
|
package/src/SuperbiaUntrue.js
DELETED
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
import $, { Component, Hook } from "untrue";
|
|
2
|
-
|
|
3
|
-
import { v4 as uuid } from "uuid";
|
|
4
|
-
|
|
5
|
-
export default class SuperbiaUntrue {
|
|
6
|
-
static useRequestKey(requestKeyExtractor = null) {
|
|
7
|
-
return Hook.useMemo(() => {
|
|
8
|
-
let key = null;
|
|
9
|
-
|
|
10
|
-
if (requestKeyExtractor !== null) {
|
|
11
|
-
if (typeof requestKeyExtractor === "function") {
|
|
12
|
-
key = requestKeyExtractor();
|
|
13
|
-
} else {
|
|
14
|
-
key = requestKeyExtractor;
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
if (key === null) {
|
|
19
|
-
key = uuid();
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
return key;
|
|
23
|
-
});
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
static wrapRequester(Child, requestKeyExtractor = null) {
|
|
27
|
-
return class RequestComponent extends Component {
|
|
28
|
-
constructor(props) {
|
|
29
|
-
super(props);
|
|
30
|
-
|
|
31
|
-
let key = null;
|
|
32
|
-
|
|
33
|
-
if (requestKeyExtractor !== null) {
|
|
34
|
-
if (typeof requestKeyExtractor === "function") {
|
|
35
|
-
key = requestKeyExtractor(props);
|
|
36
|
-
} else {
|
|
37
|
-
key = requestKeyExtractor;
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
if (key === null) {
|
|
42
|
-
key = uuid();
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
this.requestKey = key;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
render() {
|
|
49
|
-
const { children, ...props } = this.props;
|
|
50
|
-
|
|
51
|
-
return $(Child, { ...props, requestKey: this.requestKey }, children);
|
|
52
|
-
}
|
|
53
|
-
};
|
|
54
|
-
}
|
|
55
|
-
}
|