@nhost/nhost-js 0.3.5 → 0.3.6
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/package.json +5 -4
- package/src/clients/functions.ts +68 -0
- package/src/clients/graphql.ts +102 -0
- package/src/core/index.ts +1 -0
- package/src/core/nhost-client.ts +84 -0
- package/src/index.ts +9 -0
- package/src/types.ts +26 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nhost/nhost-js",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.6",
|
|
4
4
|
"description": "Nhost JavaScript SDK",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"nhost"
|
|
@@ -16,7 +16,8 @@
|
|
|
16
16
|
"access": "public"
|
|
17
17
|
},
|
|
18
18
|
"files": [
|
|
19
|
-
"dist
|
|
19
|
+
"dist",
|
|
20
|
+
"src"
|
|
20
21
|
],
|
|
21
22
|
"scripts": {
|
|
22
23
|
"build": "run-p build:esm build:cjs",
|
|
@@ -31,8 +32,8 @@
|
|
|
31
32
|
"url": "git+https://github.com/nhost/nhost-js.git"
|
|
32
33
|
},
|
|
33
34
|
"dependencies": {
|
|
34
|
-
"@nhost/hasura-auth-js": "^0.1.
|
|
35
|
-
"@nhost/hasura-storage-js": "^0.0.
|
|
35
|
+
"@nhost/hasura-auth-js": "^0.1.10",
|
|
36
|
+
"@nhost/hasura-storage-js": "^0.0.6",
|
|
36
37
|
"axios": "^0.23.0",
|
|
37
38
|
"jwt-decode": "^3.1.2",
|
|
38
39
|
"query-string": "^7.0.1"
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import axios, { AxiosInstance, AxiosRequestConfig } from 'axios';
|
|
2
|
+
import { FunctionCallResponse } from '../types';
|
|
3
|
+
|
|
4
|
+
export type NhostFunctionsConstructorParams = {
|
|
5
|
+
url: string;
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
export class NhostFunctionsClient {
|
|
9
|
+
private instance: AxiosInstance;
|
|
10
|
+
private accessToken: string | null;
|
|
11
|
+
|
|
12
|
+
constructor(params: NhostFunctionsConstructorParams) {
|
|
13
|
+
const { url } = params;
|
|
14
|
+
|
|
15
|
+
this.accessToken = null;
|
|
16
|
+
this.instance = axios.create({
|
|
17
|
+
baseURL: url,
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
public async call(
|
|
22
|
+
url: string,
|
|
23
|
+
data: any,
|
|
24
|
+
config?: AxiosRequestConfig
|
|
25
|
+
): Promise<FunctionCallResponse> {
|
|
26
|
+
const headers = {
|
|
27
|
+
...this.generateAccessTokenHeaders(),
|
|
28
|
+
...config?.headers,
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
let res;
|
|
32
|
+
try {
|
|
33
|
+
res = await this.instance.post(url, data, { ...config, headers });
|
|
34
|
+
} catch (error) {
|
|
35
|
+
if (error instanceof Error) {
|
|
36
|
+
return { res: null, error };
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (!res) {
|
|
41
|
+
return {
|
|
42
|
+
res: null,
|
|
43
|
+
error: Error('Unable to make post request to funtion'),
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return { res, error: null };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
public setAccessToken(accessToken: string | undefined) {
|
|
51
|
+
if (!accessToken) {
|
|
52
|
+
this.accessToken = null;
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
this.accessToken = accessToken;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
private generateAccessTokenHeaders() {
|
|
60
|
+
if (!this.accessToken) {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
return {
|
|
65
|
+
Authorization: `Bearer ${this.accessToken}`,
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import axios, { AxiosInstance, AxiosRequestConfig } from 'axios';
|
|
2
|
+
import { GraphqlRequestResponse, GraphqlResponse } from '../types';
|
|
3
|
+
|
|
4
|
+
export type NhostGraphqlConstructorParams = {
|
|
5
|
+
url: string;
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
export class NhostGraphqlClient {
|
|
9
|
+
private url: string;
|
|
10
|
+
private instance: AxiosInstance;
|
|
11
|
+
private accessToken: string | null;
|
|
12
|
+
|
|
13
|
+
constructor(params: NhostGraphqlConstructorParams) {
|
|
14
|
+
const { url } = params;
|
|
15
|
+
|
|
16
|
+
this.url = url;
|
|
17
|
+
this.accessToken = null;
|
|
18
|
+
this.instance = axios.create({
|
|
19
|
+
baseURL: url,
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
public async request(
|
|
24
|
+
document: string,
|
|
25
|
+
variables?: any,
|
|
26
|
+
config?: AxiosRequestConfig
|
|
27
|
+
): Promise<GraphqlRequestResponse> {
|
|
28
|
+
// add auth headers if any
|
|
29
|
+
const headers = {
|
|
30
|
+
...config?.headers,
|
|
31
|
+
...this.generateAccessTokenHeaders(),
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
const operationName = '';
|
|
35
|
+
|
|
36
|
+
let responseData;
|
|
37
|
+
try {
|
|
38
|
+
const res = await this.instance.post(
|
|
39
|
+
'',
|
|
40
|
+
{
|
|
41
|
+
operationName: operationName ? operationName : undefined,
|
|
42
|
+
query: document,
|
|
43
|
+
variables,
|
|
44
|
+
},
|
|
45
|
+
{ ...config, headers }
|
|
46
|
+
);
|
|
47
|
+
|
|
48
|
+
responseData = res.data;
|
|
49
|
+
} catch (error) {
|
|
50
|
+
if (error instanceof Error) {
|
|
51
|
+
return { data: null, error };
|
|
52
|
+
}
|
|
53
|
+
console.error(error);
|
|
54
|
+
return { data: null, error: Error('Unable to get do GraphQL request') };
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (
|
|
58
|
+
typeof responseData !== 'object' ||
|
|
59
|
+
Array.isArray(responseData) ||
|
|
60
|
+
responseData === null
|
|
61
|
+
) {
|
|
62
|
+
return {
|
|
63
|
+
data: null,
|
|
64
|
+
error: Error('incorrect response data from GraphQL server'),
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
responseData = responseData as GraphqlResponse;
|
|
69
|
+
|
|
70
|
+
if (responseData.errors) {
|
|
71
|
+
return {
|
|
72
|
+
data: null,
|
|
73
|
+
error: responseData.errors,
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return { data: responseData.data, error: null };
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
public getUrl(): string {
|
|
81
|
+
return this.url;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
public setAccessToken(accessToken: string | undefined) {
|
|
85
|
+
if (!accessToken) {
|
|
86
|
+
this.accessToken = null;
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
this.accessToken = accessToken;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
private generateAccessTokenHeaders() {
|
|
94
|
+
if (!this.accessToken) {
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
return {
|
|
99
|
+
Authorization: `Bearer ${this.accessToken}`,
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './nhost-client';
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { HasuraAuthClient } from '@nhost/hasura-auth-js';
|
|
2
|
+
import { HasuraStorageClient } from '@nhost/hasura-storage-js';
|
|
3
|
+
import { ClientStorage, ClientStorageType } from '@nhost/hasura-auth-js';
|
|
4
|
+
|
|
5
|
+
import { NhostFunctionsClient } from '../clients/functions';
|
|
6
|
+
import { NhostGraphqlClient } from '../clients/graphql';
|
|
7
|
+
|
|
8
|
+
export type NhostClientConstructorParams = {
|
|
9
|
+
backendUrl: string;
|
|
10
|
+
refreshIntervalTime?: number;
|
|
11
|
+
clientStorage?: ClientStorage;
|
|
12
|
+
clientStorageType?: ClientStorageType;
|
|
13
|
+
autoRefreshToken?: boolean;
|
|
14
|
+
autoLogin?: boolean;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export class NhostClient {
|
|
18
|
+
auth: HasuraAuthClient;
|
|
19
|
+
storage: HasuraStorageClient;
|
|
20
|
+
functions: NhostFunctionsClient;
|
|
21
|
+
graphql: NhostGraphqlClient;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Nhost Client
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* const nhost = new NhostClient({ url });
|
|
28
|
+
*
|
|
29
|
+
* @docs https://docs.nhost.io/TODO
|
|
30
|
+
*/
|
|
31
|
+
constructor(params: NhostClientConstructorParams) {
|
|
32
|
+
if (!params.backendUrl)
|
|
33
|
+
throw 'Please specify a `backendUrl`. Docs: [todo]!';
|
|
34
|
+
|
|
35
|
+
const {
|
|
36
|
+
backendUrl,
|
|
37
|
+
refreshIntervalTime,
|
|
38
|
+
clientStorage,
|
|
39
|
+
clientStorageType,
|
|
40
|
+
autoRefreshToken,
|
|
41
|
+
autoLogin,
|
|
42
|
+
} = params;
|
|
43
|
+
|
|
44
|
+
this.auth = new HasuraAuthClient({
|
|
45
|
+
url: `${backendUrl}/v1/auth`,
|
|
46
|
+
refreshIntervalTime,
|
|
47
|
+
clientStorage,
|
|
48
|
+
clientStorageType,
|
|
49
|
+
autoRefreshToken,
|
|
50
|
+
autoLogin,
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
this.storage = new HasuraStorageClient({
|
|
54
|
+
url: `${backendUrl}/v1/storage`,
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
this.functions = new NhostFunctionsClient({
|
|
58
|
+
url: `${backendUrl}/v1/functions`,
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
this.graphql = new NhostGraphqlClient({
|
|
62
|
+
url: `${backendUrl}/v1/graphql`,
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
// set current token if token is already accessable
|
|
66
|
+
this.storage.setAccessToken(this.auth.getAccessToken());
|
|
67
|
+
this.functions.setAccessToken(this.auth.getAccessToken());
|
|
68
|
+
this.graphql.setAccessToken(this.auth.getAccessToken());
|
|
69
|
+
|
|
70
|
+
// update access token for clients
|
|
71
|
+
this.auth.onAuthStateChanged((_event, session) => {
|
|
72
|
+
this.storage.setAccessToken(session?.accessToken);
|
|
73
|
+
this.functions.setAccessToken(session?.accessToken);
|
|
74
|
+
this.graphql.setAccessToken(session?.accessToken);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
// update access token for clients
|
|
78
|
+
this.auth.onTokenChanged((session) => {
|
|
79
|
+
this.storage.setAccessToken(session?.accessToken);
|
|
80
|
+
this.functions.setAccessToken(session?.accessToken);
|
|
81
|
+
this.graphql.setAccessToken(session?.accessToken);
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
}
|
package/src/index.ts
ADDED
package/src/types.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { AxiosResponse } from 'axios';
|
|
2
|
+
|
|
3
|
+
export type GraphqlRequestResponse =
|
|
4
|
+
| {
|
|
5
|
+
data: unknown;
|
|
6
|
+
error: null;
|
|
7
|
+
}
|
|
8
|
+
| {
|
|
9
|
+
data: null;
|
|
10
|
+
error: Error | object;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export type FunctionCallResponse =
|
|
14
|
+
| {
|
|
15
|
+
res: AxiosResponse;
|
|
16
|
+
error: null;
|
|
17
|
+
}
|
|
18
|
+
| {
|
|
19
|
+
res: null;
|
|
20
|
+
error: Error;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export type GraphqlResponse = {
|
|
24
|
+
errors?: object[];
|
|
25
|
+
data?: object;
|
|
26
|
+
};
|