@8ms/helpers 1.1.94 → 1.1.96
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.
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { State } from './states';
|
|
2
|
+
/**
|
|
3
|
+
* Class version of the API to enable quicker instances.
|
|
4
|
+
*/
|
|
5
|
+
declare class ApiResponse {
|
|
6
|
+
state: State;
|
|
7
|
+
body: any;
|
|
8
|
+
error: any;
|
|
9
|
+
getJson(): {
|
|
10
|
+
body: any;
|
|
11
|
+
error: any;
|
|
12
|
+
state: string;
|
|
13
|
+
};
|
|
14
|
+
isError(): boolean;
|
|
15
|
+
isIdle(): boolean;
|
|
16
|
+
isPending(): boolean;
|
|
17
|
+
isSuccess(): boolean;
|
|
18
|
+
setToIdle(): void;
|
|
19
|
+
setToPending(): void;
|
|
20
|
+
setToError({ error }: {
|
|
21
|
+
error: any;
|
|
22
|
+
}): void;
|
|
23
|
+
setToSuccess({ body }: {
|
|
24
|
+
body: any;
|
|
25
|
+
}): void;
|
|
26
|
+
setBody({ body }: {
|
|
27
|
+
body: any;
|
|
28
|
+
}): void;
|
|
29
|
+
setError({ error }: {
|
|
30
|
+
error: any;
|
|
31
|
+
}): void;
|
|
32
|
+
setState({ state }: {
|
|
33
|
+
state: State;
|
|
34
|
+
}): void;
|
|
35
|
+
}
|
|
36
|
+
export default ApiResponse;
|
|
@@ -0,0 +1,59 @@
|
|
|
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
|
+
const states_1 = __importDefault(require("./states"));
|
|
7
|
+
/**
|
|
8
|
+
* Class version of the API to enable quicker instances.
|
|
9
|
+
*/
|
|
10
|
+
class ApiResponse {
|
|
11
|
+
constructor() {
|
|
12
|
+
this.state = states_1.default.IDLE;
|
|
13
|
+
this.body = null;
|
|
14
|
+
this.error = null;
|
|
15
|
+
}
|
|
16
|
+
getJson() {
|
|
17
|
+
return {
|
|
18
|
+
body: this.body,
|
|
19
|
+
error: this.body,
|
|
20
|
+
state: this.state,
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
isError() {
|
|
24
|
+
return this.state === states_1.default.ERROR;
|
|
25
|
+
}
|
|
26
|
+
isIdle() {
|
|
27
|
+
return this.state === states_1.default.IDLE;
|
|
28
|
+
}
|
|
29
|
+
isPending() {
|
|
30
|
+
return this.state === states_1.default.PENDING;
|
|
31
|
+
}
|
|
32
|
+
isSuccess() {
|
|
33
|
+
return this.state === states_1.default.SUCCESS;
|
|
34
|
+
}
|
|
35
|
+
setToIdle() {
|
|
36
|
+
this.state = states_1.default.IDLE;
|
|
37
|
+
}
|
|
38
|
+
setToPending() {
|
|
39
|
+
this.state = states_1.default.PENDING;
|
|
40
|
+
}
|
|
41
|
+
setToError({ error }) {
|
|
42
|
+
this.error = error;
|
|
43
|
+
this.state = states_1.default.SUCCESS;
|
|
44
|
+
}
|
|
45
|
+
setToSuccess({ body }) {
|
|
46
|
+
this.body = body;
|
|
47
|
+
this.state = states_1.default.SUCCESS;
|
|
48
|
+
}
|
|
49
|
+
setBody({ body }) {
|
|
50
|
+
this.body = body;
|
|
51
|
+
}
|
|
52
|
+
setError({ error }) {
|
|
53
|
+
this.error = error;
|
|
54
|
+
}
|
|
55
|
+
setState({ state }) {
|
|
56
|
+
this.state = state;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
exports.default = ApiResponse;
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import
|
|
1
|
+
import ApiResponse from '../../api/ApiResponse';
|
|
2
2
|
declare type GetData = {
|
|
3
3
|
query: string;
|
|
4
4
|
variables?: {
|
|
5
5
|
[key: string]: any;
|
|
6
6
|
};
|
|
7
7
|
};
|
|
8
|
-
declare const getData: ({ query, variables }: GetData) => Promise<
|
|
8
|
+
declare const getData: ({ query, variables }: GetData) => Promise<ApiResponse>;
|
|
9
9
|
export default getData;
|
|
@@ -3,11 +3,10 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
const
|
|
7
|
-
const states_1 = __importDefault(require("../../api/states"));
|
|
6
|
+
const ApiResponse_1 = __importDefault(require("../../api/ApiResponse"));
|
|
8
7
|
const post_1 = __importDefault(require("../../axios/post"));
|
|
9
8
|
const getData = async ({ query, variables }) => {
|
|
10
|
-
|
|
9
|
+
const response = new ApiResponse_1.default();
|
|
11
10
|
await (0, post_1.default)({
|
|
12
11
|
config: {
|
|
13
12
|
headers: {
|
|
@@ -19,12 +18,10 @@ const getData = async ({ query, variables }) => {
|
|
|
19
18
|
variables,
|
|
20
19
|
},
|
|
21
20
|
onError: error => {
|
|
22
|
-
response.
|
|
23
|
-
response.error = error.message;
|
|
21
|
+
response.setToError({ error: error.message });
|
|
24
22
|
},
|
|
25
23
|
onSuccess: success => {
|
|
26
|
-
response.
|
|
27
|
-
response.body = success.data;
|
|
24
|
+
response.setToSuccess({ body: success.data });
|
|
28
25
|
},
|
|
29
26
|
url: 'https://graph.deepcrawl.com/',
|
|
30
27
|
});
|
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
declare type InitClient = {
|
|
2
2
|
auth: {
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
id: string;
|
|
4
|
+
secret: string;
|
|
5
5
|
};
|
|
6
6
|
};
|
|
7
7
|
/**
|
|
8
|
-
*
|
|
8
|
+
* https://graph-docs.deepcrawl.com/graphql/explorer/
|
|
9
9
|
* Renew the DeepCrawl user token.
|
|
10
|
-
* Token lasts for 6 hours.
|
|
11
10
|
*/
|
|
12
11
|
declare const initClient: ({ auth }: InitClient) => Promise<void>;
|
|
13
12
|
export default initClient;
|
|
@@ -4,30 +4,36 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
const post_1 = __importDefault(require("../../axios/post"));
|
|
7
|
+
const defaultTo_1 = __importDefault(require("../../util/defaultTo"));
|
|
7
8
|
global.deepcrawlGraphql = {
|
|
8
9
|
lastUpdated: null,
|
|
9
10
|
token: null,
|
|
10
11
|
};
|
|
11
12
|
/**
|
|
12
|
-
*
|
|
13
|
+
* https://graph-docs.deepcrawl.com/graphql/explorer/
|
|
13
14
|
* Renew the DeepCrawl user token.
|
|
14
|
-
* Token lasts for 6 hours.
|
|
15
15
|
*/
|
|
16
16
|
const initClient = async ({ auth }) => {
|
|
17
17
|
// todo: Or was more than 6 hours ago
|
|
18
18
|
if (null === global.deepcrawlGraphql.token) {
|
|
19
19
|
const apiResponse = await (0, post_1.default)({
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
20
|
+
data: {
|
|
21
|
+
query: "mutation LoginWithUserKey($secret: String!, $userKeyId: ObjectID!) { createSessionUsingUserKey(input: {userKeyId: $userKeyId, secret: $secret}) { token }}",
|
|
22
|
+
variables: {
|
|
23
|
+
userKeyId: auth.id,
|
|
24
|
+
secret: auth.secret,
|
|
24
25
|
},
|
|
25
26
|
},
|
|
26
|
-
url: `https://
|
|
27
|
+
url: `https://graph.deepcrawl.com/`,
|
|
28
|
+
});
|
|
29
|
+
const token = (0, defaultTo_1.default)({
|
|
30
|
+
defaultValue: null,
|
|
31
|
+
instance: apiResponse,
|
|
32
|
+
keys: ['data', 'data', 'createSessionUsingUserKey', 'token'],
|
|
27
33
|
});
|
|
28
34
|
// Ensure the value exists
|
|
29
|
-
if (
|
|
30
|
-
global.deepcrawlGraphql.token =
|
|
35
|
+
if (token) {
|
|
36
|
+
global.deepcrawlGraphql.token = token;
|
|
31
37
|
}
|
|
32
38
|
}
|
|
33
39
|
};
|