@dimo-network/data-sdk 1.2.1 → 1.2.3

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/dimo.test.js CHANGED
@@ -14,7 +14,6 @@ describe('Production Environment', () => {
14
14
  expect(dimo.tokenexchange.env).toBe(PROD);
15
15
  expect(dimo.trips.env).toBe(PROD);
16
16
  expect(dimo.valuations.env).toBe(PROD);
17
- expect(dimo.vehiclesignaldecoding.env).toBe(PROD);
18
17
  });
19
18
  test('Production API endpoints are defined', () => {
20
19
  expect(dimo.attestation.api).toBeDefined;
@@ -26,7 +25,6 @@ describe('Production Environment', () => {
26
25
  expect(dimo.tokenexchange.api).toBeDefined;
27
26
  expect(dimo.trips.api).toBeDefined;
28
27
  expect(dimo.valuations.api).toBeDefined;
29
- expect(dimo.vehiclesignaldecoding.api).toBeDefined;
30
28
  });
31
29
  });
32
30
  describe('Dev Environment', () => {
@@ -40,7 +38,6 @@ describe('Dev Environment', () => {
40
38
  expect(devDimo.tokenexchange.env).toBe(DEV);
41
39
  expect(devDimo.trips.env).toBe(DEV);
42
40
  expect(devDimo.valuations.env).toBe(DEV);
43
- expect(devDimo.vehiclesignaldecoding.env).toBe(DEV);
44
41
  });
45
42
  test('Dev API endpoints are defined', () => {
46
43
  expect(devDimo.attestation.api).toBeDefined;
@@ -52,6 +49,5 @@ describe('Dev Environment', () => {
52
49
  expect(devDimo.tokenexchange.api).toBeDefined;
53
50
  expect(devDimo.trips.api).toBeDefined;
54
51
  expect(devDimo.valuations.api).toBeDefined;
55
- expect(devDimo.vehiclesignaldecoding.api).toBeDefined;
56
52
  });
57
53
  });
@@ -1,2 +1,3 @@
1
- export declare const Query: (resource: any, baseUrl: any, params?: any) => Promise<any>;
1
+ import { DimoEnvironment } from '../environments';
2
+ export declare const Query: (resource: any, baseUrl: any, params: any | undefined, env: keyof typeof DimoEnvironment) => Promise<any>;
2
3
  export declare const CustomQuery: (resource: any, baseUrl: string, params?: any) => Promise<any>;
@@ -1,12 +1,13 @@
1
1
  import axios from 'axios';
2
+ import * as functionIndex from '../graphql/functions/';
2
3
  import { DimoError } from '../errors';
3
4
  // GraphQL query factory function
4
- export const Query = async (resource, baseUrl, params = {}) => {
5
+ export const Query = async (resource, baseUrl, params = {}, env) => {
5
6
  /**
6
7
  * Headers
7
8
  */
8
9
  let headers = {};
9
- if (['access_token', 'privilege_token'].includes(resource.auth)) {
10
+ if (['developer_jwt', 'vehicle_jwt'].includes(resource.auth)) {
10
11
  if (params.headers.Authorization) {
11
12
  headers = params.headers;
12
13
  }
@@ -24,6 +25,21 @@ export const Query = async (resource, baseUrl, params = {}) => {
24
25
  'User-Agent': 'dimo-node-sdk'
25
26
  }
26
27
  };
28
+ // If resource.method is 'FUNCTION', call the function defined
29
+ if (resource.method === 'FUNCTION') {
30
+ const functionName = resource.path;
31
+ const dynamicFunction = functionIndex[functionName];
32
+ if (typeof dynamicFunction === 'function') {
33
+ // Call the dynamic function with params and pass the necessary arguments
34
+ return dynamicFunction(params, env);
35
+ }
36
+ else {
37
+ throw new DimoError({
38
+ message: `Function in ${resource.path} is not a valid function.`,
39
+ statusCode: 400
40
+ });
41
+ }
42
+ }
27
43
  const variables = resource.params || {};
28
44
  let query = resource.query;
29
45
  for (const key in variables) {
@@ -64,7 +80,7 @@ export const CustomQuery = async (resource, baseUrl, params = {}) => {
64
80
  * Headers
65
81
  */
66
82
  let headers = {};
67
- if (['access_token', 'privilege_token'].includes(resource.auth)) {
83
+ if (['developer_jwt', 'vehicle_jwt'].includes(resource.auth)) {
68
84
  if (params.headers.Authorization) {
69
85
  headers = params.headers;
70
86
  }
@@ -2,6 +2,8 @@ import axios from 'axios';
2
2
  import { CustomQuery, Query } from './Query'; // Import the Query function to be tested
3
3
  import { DimoError } from '../errors';
4
4
  import { DimoEnvironment } from '../environments';
5
+ const PROD = 'Production';
6
+ const DEV = 'Dev';
5
7
  const RESOURCE = {
6
8
  method: 'POST',
7
9
  path: '',
@@ -39,9 +41,9 @@ describe('Query Function', () => {
39
41
  };
40
42
  const params = { unexpectedParam: 'value1' };
41
43
  // Call the Query function and expect it to throw an error
42
- await expect(Query(devResource, DimoEnvironment.Dev.Identity, params)).rejects.toThrow(DimoError);
43
- await expect(Query(prodResource, DimoEnvironment.Production.Identity, params)).rejects.toThrow(DimoError);
44
- await expect(Query(devResource, DimoEnvironment.Dev.Telemetry, params)).rejects.toThrow(DimoError);
45
- await expect(Query(prodResource, DimoEnvironment.Production.Telemetry, params)).rejects.toThrow(DimoError);
44
+ await expect(Query(devResource, DimoEnvironment.Dev.Identity, params, DEV)).rejects.toThrow(DimoError);
45
+ await expect(Query(prodResource, DimoEnvironment.Production.Identity, params, PROD)).rejects.toThrow(DimoError);
46
+ await expect(Query(devResource, DimoEnvironment.Dev.Telemetry, params, DEV)).rejects.toThrow(DimoError);
47
+ await expect(Query(prodResource, DimoEnvironment.Production.Telemetry, params, PROD)).rejects.toThrow(DimoError);
46
48
  });
47
49
  });
@@ -12,7 +12,8 @@ export class Resource {
12
12
  Object.keys(resources).forEach(key => {
13
13
  this[key] = (params = {}) => Query(resources[key], // Setup the endpoint resources
14
14
  this.api, // Setup the base URL
15
- params);
15
+ params, // Pass through the params
16
+ this.env);
16
17
  });
17
18
  }
18
19
  /**
@@ -0,0 +1,5 @@
1
+ import { DimoEnvironment } from '../../environments';
2
+ export declare const getVin: (input: {
3
+ headers: any;
4
+ tokenId: string;
5
+ }, env: keyof typeof DimoEnvironment) => Promise<any>;
@@ -0,0 +1,33 @@
1
+ import { DIMO } from '../../dimo';
2
+ import { DimoError } from '../../errors';
3
+ export const getVin = async (input, env) => {
4
+ const sdk = new DIMO(env);
5
+ let result;
6
+ try {
7
+ const getVinVC = await sdk.attestation.createVinVC({
8
+ headers: input.headers,
9
+ tokenId: input.tokenId
10
+ });
11
+ if (getVinVC) {
12
+ const vin = await sdk.telemetry.getLatestVinVC({
13
+ headers: input.headers,
14
+ tokenId: input.tokenId
15
+ });
16
+ result = vin.data.vinVCLatest.vin;
17
+ return result;
18
+ }
19
+ else {
20
+ throw new DimoError({
21
+ message: `Error getting attestation, make sure that you have the correct permissions.`,
22
+ statusCode: 400
23
+ });
24
+ }
25
+ }
26
+ catch (error) {
27
+ console.error(error);
28
+ throw new DimoError({
29
+ message: `Error getting VIN: ${error}`,
30
+ statusCode: 400
31
+ });
32
+ }
33
+ };
@@ -0,0 +1,2 @@
1
+ import { getVin } from "./getVin";
2
+ export { getVin };
@@ -0,0 +1,2 @@
1
+ import { getVin } from "./getVin";
2
+ export { getVin };
@@ -3,12 +3,12 @@ export class Telemetry extends Resource {
3
3
  constructor(api, env) {
4
4
  super(api, 'Telemetry', env);
5
5
  this.query({
6
- auth: 'privilege_token',
6
+ auth: 'vehicle_jwt',
7
7
  query: true,
8
8
  }),
9
9
  this.setQueries({
10
10
  getLatestSignals: {
11
- auth: 'privilege_token',
11
+ auth: 'vehicle_jwt',
12
12
  params: {
13
13
  tokenId: true
14
14
  },
@@ -33,6 +33,23 @@ export class Telemetry extends Resource {
33
33
  }
34
34
  }
35
35
  }`
36
+ },
37
+ getLatestVinVC: {
38
+ auth: 'vehicle_jwt',
39
+ params: {
40
+ tokenId: true
41
+ },
42
+ query: `
43
+ query {
44
+ vinVCLatest(tokenId: $tokenId) {
45
+ vin
46
+ }
47
+ }`
48
+ },
49
+ getVin: {
50
+ auth: 'vehicle_jwt',
51
+ method: 'FUNCTION',
52
+ path: 'getVin',
36
53
  }
37
54
  });
38
55
  }