-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetchAllRequestIdentifiers.ts
More file actions
215 lines (188 loc) · 5.45 KB
/
fetchAllRequestIdentifiers.ts
File metadata and controls
215 lines (188 loc) · 5.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import { IdentifierType } from '@transcend-io/privacy-types';
import { decodeCodec, valuesOf } from '@transcend-io/type-utils';
import type { Got } from 'got';
import { GraphQLClient } from 'graphql-request';
import * as t from 'io-ts';
import semver from 'semver';
import { SOMBRA_VERSION } from './gqls/index.js';
import { makeGraphQLRequest } from './makeGraphQLRequest.js';
const MIN_SOMBRA_VERSION_TO_DECRYPT = '7.180.0';
const RequestIdentifier = t.type({
/** ID of request */
id: t.string,
/** Name of identifier */
name: t.string,
/** The underlying identifier value */
value: t.string,
/** Type of identifier */
type: valuesOf(IdentifierType),
});
/** Type override */
export type RequestIdentifier = t.TypeOf<typeof RequestIdentifier>;
const PAGE_SIZE = 100;
const PageInfo = t.type({
endCursor: t.union([t.string, t.null]),
hasNextPage: t.boolean,
});
export const RequestIdentifiersResponse = t.type({
identifiers: t.array(RequestIdentifier),
pageInfo: PageInfo,
});
const BatchRequestIdentifier = t.type({
id: t.string,
name: t.string,
value: t.string,
type: valuesOf(IdentifierType),
requestId: t.string,
});
const BatchRequestIdentifiersResponse = t.type({
identifiers: t.array(BatchRequestIdentifier),
pageInfo: PageInfo,
});
/**
* Validate that the Sombra version meets the minimum requirement for
* decrypting request identifiers. Call once before bulk-fetching identifiers
* to avoid repeating this check on every request.
*
* @param client - GraphQL client
*/
export async function validateSombraVersion(client: GraphQLClient): Promise<void> {
const {
organization: {
sombra: { version },
},
} = await makeGraphQLRequest<{
/** The organization */
organization: {
/** Sombra */
sombra: {
/** Version string */
version: string;
};
};
}>(client, SOMBRA_VERSION);
if (version && semver.lt(version, MIN_SOMBRA_VERSION_TO_DECRYPT)) {
throw new Error(
`Please upgrade Sombra to ${MIN_SOMBRA_VERSION_TO_DECRYPT} or greater to use this command.`,
);
}
}
/**
* Fetch all request identifiers for a particular request
*
* @param client - GraphQL client
* @param sombra - Sombra client
* @param options - Options
* @returns List of request identifiers
*/
export async function fetchAllRequestIdentifiers(
client: GraphQLClient,
sombra: Got,
{
requestId,
skipSombraCheck = false,
}: {
/** ID of request to filter on */
requestId: string;
/** Skip the Sombra version check (caller already validated) */
skipSombraCheck?: boolean;
},
): Promise<RequestIdentifier[]> {
const requestIdentifiers: RequestIdentifier[] = [];
let endCursor: string | undefined;
let shouldContinue = true;
if (!skipSombraCheck) {
await validateSombraVersion(client);
}
while (shouldContinue) {
let response: unknown;
try {
response = await sombra!
.post<{
/** Decrypted identifiers */
identifiers: RequestIdentifier[];
/** Pagination info */
pageInfo: {
/** Cursor for the last item */
endCursor: string | null;
/** Whether more pages exist */
hasNextPage: boolean;
};
}>('v1/request-identifiers', {
json: {
first: PAGE_SIZE,
after: endCursor ?? undefined,
requestId,
},
})
.json();
} catch (err) {
throw new Error(
`Failed to fetch request identifiers: ${err?.response?.body || err?.message}`,
);
}
const { identifiers: nodes, pageInfo } = decodeCodec(RequestIdentifiersResponse, response);
requestIdentifiers.push(...nodes);
endCursor = pageInfo.endCursor ?? undefined;
shouldContinue = pageInfo.hasNextPage;
}
return requestIdentifiers;
}
/**
* Fetch request identifiers for multiple requests in a single paginated call.
* Returns a Map keyed by requestId so callers can look up identifiers per request.
*
* @param sombra - Sombra client
* @param options - Options
* @returns Map of requestId to its identifiers
*/
export async function fetchRequestIdentifiersBatch(
sombra: Got,
{
requestIds,
}: {
/** IDs of requests to fetch identifiers for */
requestIds: string[];
},
): Promise<Map<string, RequestIdentifier[]>> {
const result = new Map<string, RequestIdentifier[]>();
if (requestIds.length === 0) {
return result;
}
// Ensure every requested ID has an entry even if Sombra returns nothing for it
for (const id of requestIds) {
result.set(id, []);
}
let cursor: string | undefined;
let shouldContinue = true;
while (shouldContinue) {
let response: unknown;
try {
response = await sombra
.post('v1/request-identifiers', {
json: {
first: PAGE_SIZE,
after: cursor ?? undefined,
requestIds,
},
})
.json();
} catch (err) {
throw new Error(
`Failed to fetch request identifiers: ${err?.response?.body || err?.message}`,
);
}
const { identifiers: nodes, pageInfo } = decodeCodec(BatchRequestIdentifiersResponse, response);
for (const { requestId, ...identifier } of nodes) {
const list = result.get(requestId);
if (list) {
list.push(identifier);
} else {
result.set(requestId, [identifier]);
}
}
cursor = pageInfo.endCursor ?? undefined;
shouldContinue = pageInfo.hasNextPage;
}
return result;
}