@cumulus/api-client 15.0.3 → 16.0.0

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/src/executions.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { ExecutionRecord } from '@cumulus/types/api/executions';
1
+ import { ApiExecutionRecord } from '@cumulus/types/api/executions';
2
2
  import { invokeApi } from './cumulusApiClient';
3
3
  import { ApiGatewayLambdaHttpProxyResponse, InvokeApiFunction } from './types';
4
4
 
@@ -17,7 +17,7 @@ export const getExecution = async (params: {
17
17
  prefix: string,
18
18
  arn: string,
19
19
  callback?: InvokeApiFunction
20
- }): Promise<ExecutionRecord> => {
20
+ }): Promise<ApiExecutionRecord> => {
21
21
  const { prefix, arn, callback = invokeApi } = params;
22
22
 
23
23
  const response = await callback({
@@ -105,7 +105,7 @@ export const getExecutionStatus = async (params: {
105
105
  */
106
106
  export const createExecution = async (params: {
107
107
  prefix: string,
108
- body: ExecutionRecord,
108
+ body: ApiExecutionRecord,
109
109
  callback?: InvokeApiFunction
110
110
  }): Promise<ApiGatewayLambdaHttpProxyResponse> => {
111
111
  const { prefix, body, callback = invokeApi } = params;
@@ -136,7 +136,7 @@ export const createExecution = async (params: {
136
136
  */
137
137
  export const updateExecution = async (params: {
138
138
  prefix: string,
139
- body: ExecutionRecord,
139
+ body: ApiExecutionRecord,
140
140
  callback?: InvokeApiFunction
141
141
  }): Promise<ApiGatewayLambdaHttpProxyResponse> => {
142
142
  const { prefix, body, callback = invokeApi } = params;
package/src/granules.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import pRetry from 'p-retry';
2
2
 
3
- import { ApiGranuleRecord, GranuleId, GranuleStatus } from '@cumulus/types/api/granules';
3
+ import { ApiGranuleRecord, ApiGranule, GranuleId, GranuleStatus } from '@cumulus/types/api/granules';
4
+ import { CollectionId } from '@cumulus/types/api/collections';
4
5
  import Logger from '@cumulus/logger';
5
6
 
6
7
  import { invokeApi } from './cumulusApiClient';
@@ -15,44 +16,59 @@ type AssociateExecutionRequest = {
15
16
  };
16
17
 
17
18
  /**
18
- * GET raw response from /granules/{granuleName}
19
+ * GET raw response from /granules/{granuleId} or /granules/{collectionId}/{granuleId}
19
20
  *
20
- * @param {Object} params - params
21
- * @param {string} params.prefix - the prefix configured for the stack
22
- * @param {string} params.granuleId - a granule ID
23
- * @param {Object} [params.query] - query to pass the API lambda
24
- * @param {Function} params.callback - async function to invoke the api lambda
25
- * that takes a prefix / user payload. Defaults
26
- * to cumulusApiClient.invokeApifunction to invoke the
27
- * api lambda
28
- * @returns {Promise<Object>} - the granule fetched by the API
21
+ * @param {Object} params - params
22
+ * @param {string} params.prefix - the prefix configured for the stack
23
+ * @param {string} params.granuleId - a granule ID
24
+ * @param {Object} [params.query] - query to pass the API lambda
25
+ * @param {number[] | number} params.expectedStatusCodes - the statusCodes which the granule API is
26
+ * is expecting for the invokeApi Response,
27
+ * default is 200
28
+ * @param {Function} params.callback - async function to invoke the api lambda
29
+ * that takes a prefix / user payload,
30
+ * cumulusApiClient.invokeApifunction
31
+ * is the default to invoke the api lambda
32
+ * @returns {Promise<Object>} - the granule fetched by the API
29
33
  */
30
34
  export const getGranuleResponse = async (params: {
31
35
  prefix: string,
32
36
  granuleId: GranuleId,
37
+ collectionId?: CollectionId,
38
+ expectedStatusCodes?: number[] | number,
33
39
  query?: { [key: string]: string },
34
40
  callback?: InvokeApiFunction
35
41
  }): Promise<ApiGatewayLambdaHttpProxyResponse> => {
36
42
  const {
37
43
  prefix,
38
44
  granuleId,
45
+ collectionId,
39
46
  query,
47
+ expectedStatusCodes,
40
48
  callback = invokeApi,
41
49
  } = params;
42
50
 
51
+ let path = `/granules/${collectionId}/${granuleId}`;
52
+
53
+ // Fetching a granule without a collectionId is supported but deprecated
54
+ if (!collectionId) {
55
+ path = `/granules/${granuleId}`;
56
+ }
57
+
43
58
  return await callback({
44
- prefix: prefix,
59
+ prefix,
45
60
  payload: {
46
61
  httpMethod: 'GET',
47
62
  resource: '/{proxy+}',
48
- path: `/granules/${granuleId}`,
63
+ path,
49
64
  ...(query && { queryStringParameters: query }),
50
65
  },
66
+ expectedStatusCodes,
51
67
  });
52
68
  };
53
69
 
54
70
  /**
55
- * GET granule record from /granules/{granuleName}
71
+ * GET granule record from /granules/{granuleId} or /granules/{collectionId}/{granuleId}
56
72
  *
57
73
  * @param {Object} params - params
58
74
  * @param {string} params.prefix - the prefix configured for the stack
@@ -67,6 +83,7 @@ export const getGranuleResponse = async (params: {
67
83
  export const getGranule = async (params: {
68
84
  prefix: string,
69
85
  granuleId: GranuleId,
86
+ collectionId?: CollectionId,
70
87
  query?: { [key: string]: string },
71
88
  callback?: InvokeApiFunction
72
89
  }): Promise<ApiGranuleRecord> => {
@@ -104,6 +121,7 @@ export const waitForGranule = async (params: {
104
121
 
105
122
  await pRetry(
106
123
  async () => {
124
+ // TODO update to use collectionId + granuleId
107
125
  const apiResult = await getGranuleResponse({ prefix, granuleId, callback });
108
126
 
109
127
  if (apiResult.statusCode === 500) {
@@ -152,20 +170,36 @@ export const waitForGranule = async (params: {
152
170
  export const reingestGranule = async (params: {
153
171
  prefix: string,
154
172
  granuleId: GranuleId,
173
+ collectionId?: CollectionId,
155
174
  workflowName?: string | undefined,
156
175
  executionArn?: string | undefined,
157
176
  callback?: InvokeApiFunction
158
177
  }): Promise<ApiGatewayLambdaHttpProxyResponse> => {
159
- const { prefix, granuleId, workflowName, executionArn, callback = invokeApi } = params;
178
+ const {
179
+ prefix,
180
+ granuleId,
181
+ collectionId,
182
+ workflowName,
183
+ executionArn,
184
+ callback = invokeApi,
185
+ } = params;
186
+
187
+ let path = `/granules/${collectionId}/${granuleId}`;
188
+
189
+ // Fetching a granule without a collectionId is supported but deprecated
190
+ if (!collectionId) {
191
+ path = `/granules/${granuleId}`;
192
+ }
160
193
 
161
194
  return await callback({
162
195
  prefix: prefix,
163
196
  payload: {
164
197
  httpMethod: 'PATCH',
165
198
  resource: '/{proxy+}',
166
- path: `/granules/${granuleId}`,
199
+ path,
167
200
  headers: {
168
201
  'Content-Type': 'application/json',
202
+ 'Cumulus-API-Version': '2',
169
203
  },
170
204
  body: JSON.stringify({
171
205
  action: 'reingest',
@@ -192,18 +226,27 @@ export const reingestGranule = async (params: {
192
226
  export const removeFromCMR = async (params: {
193
227
  prefix: string,
194
228
  granuleId: GranuleId,
229
+ collectionId?: CollectionId,
195
230
  callback?: InvokeApiFunction
196
231
  }): Promise<ApiGatewayLambdaHttpProxyResponse> => {
197
- const { prefix, granuleId, callback = invokeApi } = params;
232
+ const { prefix, granuleId, collectionId, callback = invokeApi } = params;
233
+
234
+ let path = `/granules/${collectionId}/${granuleId}`;
235
+
236
+ // Fetching a granule without a collectionId is supported but deprecated
237
+ if (!collectionId) {
238
+ path = `/granules/${granuleId}`;
239
+ }
198
240
 
199
241
  return await callback({
200
242
  prefix: prefix,
201
243
  payload: {
202
244
  httpMethod: 'PATCH',
203
245
  resource: '/{proxy+}',
204
- path: `/granules/${granuleId}`,
246
+ path,
205
247
  headers: {
206
248
  'Content-Type': 'application/json',
249
+ 'Cumulus-API-Version': '2',
207
250
  },
208
251
  body: JSON.stringify({ action: 'removeFromCmr' }),
209
252
  },
@@ -227,6 +270,7 @@ export const removeFromCMR = async (params: {
227
270
  export const applyWorkflow = async (params: {
228
271
  prefix: string,
229
272
  granuleId: GranuleId,
273
+ collectionId?: CollectionId,
230
274
  workflow: string,
231
275
  meta?: object,
232
276
  callback?: InvokeApiFunction
@@ -234,11 +278,19 @@ export const applyWorkflow = async (params: {
234
278
  const {
235
279
  prefix,
236
280
  granuleId,
281
+ collectionId,
237
282
  workflow,
238
283
  meta,
239
284
  callback = invokeApi,
240
285
  } = params;
241
286
 
287
+ let path = `/granules/${collectionId}/${granuleId}`;
288
+
289
+ // Fetching a granule without a collectionId is supported but deprecated
290
+ if (!collectionId) {
291
+ path = `/granules/${granuleId}`;
292
+ }
293
+
242
294
  return await callback({
243
295
  prefix: prefix,
244
296
  payload: {
@@ -246,8 +298,9 @@ export const applyWorkflow = async (params: {
246
298
  resource: '/{proxy+}',
247
299
  headers: {
248
300
  'Content-Type': 'application/json',
301
+ 'Cumulus-API-Version': '2',
249
302
  },
250
- path: `/granules/${granuleId}`,
303
+ path,
251
304
  body: JSON.stringify({ action: 'applyWorkflow', workflow, meta }),
252
305
  },
253
306
  });
@@ -270,6 +323,7 @@ export const applyWorkflow = async (params: {
270
323
  export const deleteGranule = async (params: {
271
324
  prefix: string,
272
325
  granuleId: GranuleId,
326
+ collectionId?: CollectionId,
273
327
  pRetryOptions?: pRetry.Options,
274
328
  callback?: InvokeApiFunction
275
329
  }): Promise<ApiGatewayLambdaHttpProxyResponse> => {
@@ -277,14 +331,23 @@ export const deleteGranule = async (params: {
277
331
  pRetryOptions,
278
332
  prefix,
279
333
  granuleId,
334
+ collectionId,
280
335
  callback = invokeApi,
281
336
  } = params;
337
+
338
+ let path = `/granules/${collectionId}/${granuleId}`;
339
+
340
+ // Fetching a granule without a collectionId is supported but deprecated
341
+ if (!collectionId) {
342
+ path = `/granules/${granuleId}`;
343
+ }
344
+
282
345
  return await callback({
283
346
  prefix: prefix,
284
347
  payload: {
285
348
  httpMethod: 'DELETE',
286
349
  resource: '/{proxy+}',
287
- path: `/granules/${granuleId}`,
350
+ path,
288
351
  },
289
352
  pRetryOptions,
290
353
  });
@@ -307,16 +370,25 @@ export const deleteGranule = async (params: {
307
370
  export const moveGranule = async (params: {
308
371
  prefix: string,
309
372
  granuleId: GranuleId,
373
+ collectionId?: CollectionId,
310
374
  destinations: unknown[],
311
375
  callback?: InvokeApiFunction
312
376
  }): Promise<ApiGatewayLambdaHttpProxyResponse> => {
313
377
  const {
314
378
  prefix,
315
379
  granuleId,
380
+ collectionId,
316
381
  destinations,
317
382
  callback = invokeApi,
318
383
  } = params;
319
384
 
385
+ let path = `/granules/${collectionId}/${granuleId}`;
386
+
387
+ // Fetching a granule without a collectionId is supported but deprecated
388
+ if (!collectionId) {
389
+ path = `/granules/${granuleId}`;
390
+ }
391
+
320
392
  return await callback({
321
393
  prefix: prefix,
322
394
  payload: {
@@ -324,8 +396,9 @@ export const moveGranule = async (params: {
324
396
  resource: '/{proxy+}',
325
397
  headers: {
326
398
  'Content-Type': 'application/json',
399
+ 'Cumulus-API-Version': '2',
327
400
  },
328
- path: `/granules/${granuleId}`,
401
+ path,
329
402
  body: JSON.stringify({ action: 'move', destinations }),
330
403
  },
331
404
  });
@@ -346,13 +419,14 @@ export const moveGranule = async (params: {
346
419
  export const removePublishedGranule = async (params: {
347
420
  prefix: string,
348
421
  granuleId: GranuleId,
422
+ collectionId?: CollectionId,
349
423
  callback?: InvokeApiFunction
350
424
  }): Promise<ApiGatewayLambdaHttpProxyResponse> => {
351
- const { prefix, granuleId, callback = invokeApi } = params;
425
+ const { prefix, granuleId, collectionId, callback = invokeApi } = params;
352
426
 
353
427
  // pre-delete: Remove the granule from CMR
354
- await removeFromCMR({ prefix, granuleId, callback });
355
- return deleteGranule({ prefix, granuleId, callback });
428
+ await removeFromCMR({ prefix, granuleId, collectionId, callback });
429
+ return deleteGranule({ prefix, granuleId, collectionId, callback });
356
430
  };
357
431
 
358
432
  /**
@@ -417,6 +491,42 @@ export const createGranule = async (params: {
417
491
  });
418
492
  };
419
493
 
494
+ /**
495
+ * Update/create granule in cumulus via PUT request. Existing values will
496
+ * be removed if not specified and in some cases replaced with defaults.
497
+ * Granule execution association history will be retained.
498
+ * PUT /granules/{collectionId}/{granuleId}
499
+ * @param {Object} params - params
500
+ * @param {Object} [params.body] - granule to pass the API lambda
501
+ * @param {Function} params.callback - async function to invoke the api lambda
502
+ * that takes a prefix / user payload. Defaults
503
+ * to cumulusApiClient.invokeApifunction to invoke the
504
+ * api lambda
505
+ * @returns {Promise<Object>} - the response from the callback
506
+ */
507
+ export const replaceGranule = async (params: {
508
+ prefix: string,
509
+ body: ApiGranuleRecord,
510
+ callback?: InvokeApiFunction
511
+ }): Promise<ApiGatewayLambdaHttpProxyResponse> => {
512
+ const { prefix, body, callback = invokeApi } = params;
513
+
514
+ return await callback({
515
+ prefix,
516
+ payload: {
517
+ httpMethod: 'PUT',
518
+ resource: '/{proxy+}',
519
+ path: `/granules/${body.collectionId}/${body.granuleId}`,
520
+ headers: {
521
+ 'Content-Type': 'application/json',
522
+ 'Cumulus-API-Version': '2',
523
+ },
524
+ body: JSON.stringify(body),
525
+ },
526
+ expectedStatusCodes: [200, 201],
527
+ });
528
+ };
529
+
420
530
  /**
421
531
  * Update granule in cumulus via PATCH request. Existing values will
422
532
  * not be overwritten if not specified, null values will be removed and in
@@ -433,17 +543,26 @@ export const createGranule = async (params: {
433
543
  export const updateGranule = async (params: {
434
544
  prefix: string,
435
545
  body: ApiGranuleRecord,
546
+ granuleId: GranuleId,
547
+ collectionId?: CollectionId,
436
548
  callback?: InvokeApiFunction
437
549
  }): Promise<ApiGatewayLambdaHttpProxyResponse> => {
438
- const { prefix, body, callback = invokeApi } = params;
550
+ const { prefix, granuleId, collectionId, body, callback = invokeApi } = params;
551
+
552
+ let path = `/granules/${collectionId}/${granuleId}`;
553
+
554
+ // Fetching a granule without a collectionId is supported but deprecated
555
+ if (!collectionId) {
556
+ path = `/granules/${granuleId}`;
557
+ }
439
558
 
440
559
  return await callback({
441
560
  prefix,
442
561
  payload: {
443
562
  httpMethod: 'PATCH',
444
563
  resource: '/{proxy+}',
445
- path: `/granules/${body.granuleId}`,
446
- headers: { 'Content-Type': 'application/json' },
564
+ path,
565
+ headers: { 'Content-Type': 'application/json', 'Cumulus-API-Version': '2' },
447
566
  body: JSON.stringify(body),
448
567
  },
449
568
  expectedStatusCodes: [200, 201],
@@ -493,7 +612,7 @@ export const associateExecutionWithGranule = async (params: {
493
612
  */
494
613
  export const bulkGranules = async (params: {
495
614
  prefix: string,
496
- body: object,
615
+ body: unknown,
497
616
  callback?: InvokeApiFunction
498
617
  }): Promise<ApiGatewayLambdaHttpProxyResponse> => {
499
618
  const { prefix, body, callback = invokeApi } = params;
@@ -574,7 +693,7 @@ export const bulkReingestGranules = async (params: {
574
693
  *
575
694
  * @param {Object} params - params
576
695
  * @param {string} params.prefix - the prefix configured for the stack
577
- * @param {Array<string>} params.ids - the granules to have bulk operation on
696
+ * @param {Array<ApiGranuleRecord>} params.granules - the granules to have bulk operation on
578
697
  * @param {string} params.workflowName - workflowName for the bulk operation execution
579
698
  * @param {Function} params.callback - async function to invoke the api lambda
580
699
  * that takes a prefix / user payload. Defaults
@@ -584,11 +703,11 @@ export const bulkReingestGranules = async (params: {
584
703
  */
585
704
  export const bulkOperation = async (params: {
586
705
  prefix: string,
587
- ids: string[],
706
+ granules: ApiGranule[],
588
707
  workflowName: string,
589
708
  callback?: InvokeApiFunction
590
709
  }): Promise<ApiGatewayLambdaHttpProxyResponse> => {
591
- const { prefix, ids, workflowName, callback = invokeApi } = params;
710
+ const { prefix, granules, workflowName, callback = invokeApi } = params;
592
711
  return await callback({
593
712
  prefix: prefix,
594
713
  payload: {
@@ -598,7 +717,7 @@ export const bulkOperation = async (params: {
598
717
  headers: {
599
718
  'Content-Type': 'application/json',
600
719
  },
601
- body: JSON.stringify({ ids, workflowName }),
720
+ body: JSON.stringify({ granules, workflowName }),
602
721
  },
603
722
  expectedStatusCodes: 202,
604
723
  });