@nhost/nhost-js 0.3.6 → 0.3.7

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.
Files changed (47) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +4 -5
  3. package/dist/{cjs/clients → clients}/functions.d.ts +2 -2
  4. package/dist/{esm/clients → clients}/graphql.d.ts +2 -2
  5. package/dist/{cjs/core → core}/index.d.ts +0 -0
  6. package/dist/{esm/core → core}/nhost-client.d.ts +3 -12
  7. package/dist/{cjs/index.d.ts → index.d.ts} +1 -2
  8. package/dist/index.es.js +4507 -0
  9. package/dist/index.umd.js +4 -0
  10. package/dist/{cjs/types.d.ts → types.d.ts} +5 -5
  11. package/package.json +45 -34
  12. package/src/index.ts +4 -7
  13. package/dist/cjs/clients/functions.js +0 -113
  14. package/dist/cjs/clients/functions.js.map +0 -1
  15. package/dist/cjs/clients/graphql.d.ts +0 -15
  16. package/dist/cjs/clients/graphql.js +0 -133
  17. package/dist/cjs/clients/graphql.js.map +0 -1
  18. package/dist/cjs/core/index.js +0 -14
  19. package/dist/cjs/core/index.js.map +0 -1
  20. package/dist/cjs/core/nhost-client.d.ts +0 -28
  21. package/dist/cjs/core/nhost-client.js +0 -59
  22. package/dist/cjs/core/nhost-client.js.map +0 -1
  23. package/dist/cjs/index.js +0 -20
  24. package/dist/cjs/index.js.map +0 -1
  25. package/dist/cjs/types.js +0 -3
  26. package/dist/cjs/types.js.map +0 -1
  27. package/dist/esm/clients/functions.d.ts +0 -13
  28. package/dist/esm/clients/functions.js +0 -107
  29. package/dist/esm/clients/functions.js.map +0 -1
  30. package/dist/esm/clients/graphql.js +0 -127
  31. package/dist/esm/clients/graphql.js.map +0 -1
  32. package/dist/esm/core/index.d.ts +0 -1
  33. package/dist/esm/core/index.js +0 -2
  34. package/dist/esm/core/index.js.map +0 -1
  35. package/dist/esm/core/nhost-client.js +0 -56
  36. package/dist/esm/core/nhost-client.js.map +0 -1
  37. package/dist/esm/index.d.ts +0 -5
  38. package/dist/esm/index.js +0 -7
  39. package/dist/esm/index.js.map +0 -1
  40. package/dist/esm/types.d.ts +0 -19
  41. package/dist/esm/types.js +0 -2
  42. package/dist/esm/types.js.map +0 -1
  43. package/src/clients/functions.ts +0 -68
  44. package/src/clients/graphql.ts +0 -102
  45. package/src/core/index.ts +0 -1
  46. package/src/core/nhost-client.ts +0 -84
  47. package/src/types.ts +0 -26
@@ -1,102 +0,0 @@
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
- }
package/src/core/index.ts DELETED
@@ -1 +0,0 @@
1
- export * from './nhost-client';
@@ -1,84 +0,0 @@
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/types.ts DELETED
@@ -1,26 +0,0 @@
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
- };