@dimo-network/data-sdk 1.2.3 → 1.2.5

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.
@@ -1,4 +1,5 @@
1
1
  import { Resource } from '../../Resource';
2
+ import { DimoConstants } from '../../../constants';
2
3
  export class Identity extends Resource {
3
4
  constructor(api, env) {
4
5
  super(api, 'Identity', env);
@@ -6,14 +7,41 @@ export class Identity extends Resource {
6
7
  query: true
7
8
  }),
8
9
  this.setQueries({
9
- countDimoVehicles: {
10
+ getVehiclePrivileges: {
11
+ method: 'FUNCTION',
12
+ path: 'getVehiclePrivileges',
13
+ },
14
+ listSacdPerVehicleTokenId: {
15
+ params: {
16
+ tokenId: true,
17
+ after: true
18
+ },
10
19
  query: `
11
- {
12
- vehicles (first:10) {
13
- totalCount,
14
- }
20
+ {
21
+ vehicle(tokenId: $tokenId) {
22
+ sacds(first: ${DimoConstants.Query.PAGE_SIZE}, after: $after) {
23
+ nodes {
24
+ permissions
25
+ grantee
26
+ }
27
+ totalCount
28
+ pageInfo {
29
+ startCursor
30
+ endCursor
31
+ }
15
32
  }
16
- `
33
+ }
34
+ }
35
+ `
36
+ },
37
+ countDimoVehicles: {
38
+ query: `
39
+ {
40
+ vehicles (first: ${DimoConstants.Query.PAGE_SIZE}) {
41
+ totalCount,
42
+ }
43
+ }
44
+ `
17
45
  },
18
46
  listVehicleDefinitionsPerAddress: {
19
47
  params: {
@@ -21,80 +49,27 @@ export class Identity extends Resource {
21
49
  limit: true
22
50
  },
23
51
  query: `
24
- {
25
- vehicles(filterBy: {owner: $address}, first: $limit) {
26
- nodes {
27
- aftermarketDevice {
28
- tokenId
29
- address
30
- }
31
- syntheticDevice {
32
- address
33
- tokenId
34
- }
35
- definition {
36
- make
37
- model
38
- year
39
- }
40
- }
41
- }
52
+ {
53
+ vehicles(filterBy: {owner: $address}, first: $limit) {
54
+ nodes {
55
+ aftermarketDevice {
56
+ tokenId
57
+ address
58
+ }
59
+ syntheticDevice {
60
+ address
61
+ tokenId
42
62
  }
43
- `
63
+ definition {
64
+ make
65
+ model
66
+ year
67
+ }
68
+ }
69
+ }
70
+ }
71
+ `
44
72
  }
45
73
  });
46
74
  }
47
75
  }
48
- export const listVehicleDefinitionsPerAddress = (address, limit) => `
49
- {
50
- vehicles(filterBy: {owner: "${address}"}, first: ${limit}) {
51
- nodes {
52
- aftermarketDevice {
53
- tokenId
54
- address
55
- }
56
- syntheticDevice {
57
- address
58
- tokenId
59
- }
60
- definition {
61
- make
62
- model
63
- year
64
- }
65
- }
66
- }
67
- }
68
- `;
69
- // export const getVehicleDetailsByTokenId = (tokenId: number) => `
70
- // {
71
- // vehicle (tokenId: ${tokenId}) {
72
- // aftermarketDevice {
73
- // tokenId
74
- // address
75
- // }
76
- // syntheticDevice {
77
- // address
78
- // tokenId
79
- // }
80
- // definition {
81
- // make
82
- // model
83
- // year
84
- // }
85
- // }
86
- // }
87
- // `;
88
- // export const test = () => `
89
- // {
90
- // vehicles(filterBy: {owner: "0xf9D26323Ab49179A6d57C26515B01De018553787"}, first: 10) {
91
- // nodes {
92
- // definition {
93
- // make
94
- // model
95
- // year
96
- // }
97
- // }
98
- // }
99
- // }
100
- // `;
@@ -0,0 +1,9 @@
1
+ export declare const paginate: <T>(fetchPage: (after: string | undefined) => Promise<{
2
+ nodes: {
3
+ nodes: T[];
4
+ };
5
+ pageInfo: {
6
+ endCursor: string | null;
7
+ };
8
+ totalCount: number | null;
9
+ }>) => Promise<T[]>;
@@ -0,0 +1,37 @@
1
+ import { DimoConstants } from '../../constants';
2
+ const pageSize = DimoConstants.Query.PAGE_SIZE;
3
+ export const paginate = async (fetchPage) => {
4
+ let after = null;
5
+ let result = [];
6
+ let totalCount = null;
7
+ let page = 0;
8
+ while (true) {
9
+ const response = await fetchPage(after ?? undefined);
10
+ const { nodes, pageInfo, totalCount: responseTotalCount } = response;
11
+ if (!nodes || !nodes.nodes) {
12
+ console.error('Invalid response: nodes is undefined or null', response);
13
+ throw new Error('Unexpected API response format');
14
+ }
15
+ // Capture totalCount from the first request
16
+ if (totalCount === null) {
17
+ totalCount = responseTotalCount ?? null; // Ensure fallback if totalCount is missing
18
+ if (totalCount === null) {
19
+ console.warn('Warning: totalCount is null. Defaulting to single page execution.');
20
+ totalCount = pageSize; // Assume at least one page if totalCount is unknown
21
+ }
22
+ }
23
+ result.push(...nodes.nodes);
24
+ page++;
25
+ const totalPages = Math.ceil(totalCount / pageSize);
26
+ if (page >= totalPages || nodes.nodes.length < pageSize) {
27
+ break;
28
+ }
29
+ // Update the cursor
30
+ after = pageInfo.endCursor ?? null;
31
+ if (after === null && page < totalPages) {
32
+ console.warn(`Expected more pages (${page}/${totalPages}), but endCursor is null. Stopping pagination.`);
33
+ break;
34
+ }
35
+ }
36
+ return result;
37
+ };