@hello.nrfcloud.com/nrfcloud-api-helpers 5.0.10 → 5.1.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.
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { describe, it, mock } from 'node:test';
|
|
2
|
+
import assert from 'node:assert/strict';
|
|
3
|
+
import { getFOTABundles } from './getFOTABundles.js';
|
|
4
|
+
void describe('getFOTABundles()', () => {
|
|
5
|
+
void it('should fetch all FOTA bundles', async () => {
|
|
6
|
+
const mockFetch = mock.fn();
|
|
7
|
+
mockFetch.mock.mockImplementationOnce(() => ({
|
|
8
|
+
ok: true,
|
|
9
|
+
json: async () => Promise.resolve({
|
|
10
|
+
items: [
|
|
11
|
+
{
|
|
12
|
+
bundleId: 'APP*0038b655*v1.1.1-debug',
|
|
13
|
+
lastModified: '2023-06-28T09:50:02.000Z',
|
|
14
|
+
size: 418565,
|
|
15
|
+
version: 'v1.1.1-debug',
|
|
16
|
+
type: 'APP',
|
|
17
|
+
filenames: ['hello-nrfcloud-thingy91-debug-v1.1.1-fwupd.bin'],
|
|
18
|
+
name: 'hello.nrfcloud.com v1.1.1-debug',
|
|
19
|
+
description: 'Firmware Update Image BIN file (thingy91, debug)',
|
|
20
|
+
},
|
|
21
|
+
],
|
|
22
|
+
total: 1,
|
|
23
|
+
pageNextToken: '102/B5fGZNAs7vcw8E2i611ID4apx/Du/2/H6nr2UDWk5eoihEeAgh6qoaGcDzAI4M8JCoO4iAAK96TWfuB19ru9c1PrnwiTUdw/sZzwrYSrS433vPjDJNvJUIEmqm9+V3ElM5M1bLmt6GrGa57SymHHK4nN0W+zHhmE97cCCfzJMBXhVTl3TzvBx5rE1KJYf',
|
|
24
|
+
}),
|
|
25
|
+
}), 0);
|
|
26
|
+
mockFetch.mock.mockImplementationOnce(() => ({
|
|
27
|
+
ok: true,
|
|
28
|
+
json: async () => Promise.resolve({
|
|
29
|
+
items: [
|
|
30
|
+
{
|
|
31
|
+
bundleId: 'APP*0103b0f9*v1.1.2-sol-dbg',
|
|
32
|
+
lastModified: '2023-06-29T14:18:19.000Z',
|
|
33
|
+
size: 426280,
|
|
34
|
+
version: 'v1.1.2-sol-dbg',
|
|
35
|
+
type: 'APP',
|
|
36
|
+
filenames: ['hello-nrfcloud-thingy91-sol-dbg-v1.1.2-fwupd.bin'],
|
|
37
|
+
name: 'hello.nrfcloud.com v1.1.2-sol-dbg',
|
|
38
|
+
description: 'Firmware Update Image BIN file (thingy91, solar, debug)',
|
|
39
|
+
},
|
|
40
|
+
],
|
|
41
|
+
total: 1,
|
|
42
|
+
}),
|
|
43
|
+
}), 1);
|
|
44
|
+
const fetcher = getFOTABundles({
|
|
45
|
+
endpoint: new URL('https://example.com/'),
|
|
46
|
+
apiKey: 'some-key',
|
|
47
|
+
}, mockFetch);
|
|
48
|
+
const res = await fetcher();
|
|
49
|
+
assert.deepEqual(mockFetch.mock.calls[0]?.arguments, [
|
|
50
|
+
`https://example.com/v1/firmwares?pageLimit=100`,
|
|
51
|
+
{
|
|
52
|
+
headers: {
|
|
53
|
+
Accept: 'application/json; charset=utf-8',
|
|
54
|
+
Authorization: 'Bearer some-key',
|
|
55
|
+
},
|
|
56
|
+
},
|
|
57
|
+
]);
|
|
58
|
+
assert.deepEqual(mockFetch.mock.calls[1]?.arguments, [
|
|
59
|
+
`https://example.com/v1/firmwares?pageLimit=100&pageNextToken=${encodeURIComponent(`102/B5fGZNAs7vcw8E2i611ID4apx/Du/2/H6nr2UDWk5eoihEeAgh6qoaGcDzAI4M8JCoO4iAAK96TWfuB19ru9c1PrnwiTUdw/sZzwrYSrS433vPjDJNvJUIEmqm9+V3ElM5M1bLmt6GrGa57SymHHK4nN0W+zHhmE97cCCfzJMBXhVTl3TzvBx5rE1KJYf`)}`,
|
|
60
|
+
{
|
|
61
|
+
headers: {
|
|
62
|
+
Accept: 'application/json; charset=utf-8',
|
|
63
|
+
Authorization: 'Bearer some-key',
|
|
64
|
+
},
|
|
65
|
+
},
|
|
66
|
+
]);
|
|
67
|
+
assert.deepEqual(res, {
|
|
68
|
+
bundles: [
|
|
69
|
+
{
|
|
70
|
+
bundleId: 'APP*0038b655*v1.1.1-debug',
|
|
71
|
+
lastModified: '2023-06-28T09:50:02.000Z',
|
|
72
|
+
size: 418565,
|
|
73
|
+
version: 'v1.1.1-debug',
|
|
74
|
+
type: 'APP',
|
|
75
|
+
filenames: ['hello-nrfcloud-thingy91-debug-v1.1.1-fwupd.bin'],
|
|
76
|
+
name: 'hello.nrfcloud.com v1.1.1-debug',
|
|
77
|
+
description: 'Firmware Update Image BIN file (thingy91, debug)',
|
|
78
|
+
},
|
|
79
|
+
{
|
|
80
|
+
bundleId: 'APP*0103b0f9*v1.1.2-sol-dbg',
|
|
81
|
+
lastModified: '2023-06-29T14:18:19.000Z',
|
|
82
|
+
size: 426280,
|
|
83
|
+
version: 'v1.1.2-sol-dbg',
|
|
84
|
+
type: 'APP',
|
|
85
|
+
filenames: ['hello-nrfcloud-thingy91-sol-dbg-v1.1.2-fwupd.bin'],
|
|
86
|
+
name: 'hello.nrfcloud.com v1.1.2-sol-dbg',
|
|
87
|
+
description: 'Firmware Update Image BIN file (thingy91, solar, debug)',
|
|
88
|
+
},
|
|
89
|
+
],
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
});
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { type Static } from '@sinclair/typebox';
|
|
2
|
+
import type { ValidationError } from 'ajv';
|
|
3
|
+
import { FwType } from './devices.js';
|
|
4
|
+
export declare const FOTABundle: import("@sinclair/typebox").TObject<{
|
|
5
|
+
bundleId: import("@sinclair/typebox").TString;
|
|
6
|
+
lastModified: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TString>;
|
|
7
|
+
size: import("@sinclair/typebox").TNumber;
|
|
8
|
+
version: import("@sinclair/typebox").TString;
|
|
9
|
+
type: import("@sinclair/typebox").TEnum<typeof FwType>;
|
|
10
|
+
filenames: import("@sinclair/typebox").TArray<import("@sinclair/typebox").TString>;
|
|
11
|
+
name: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TString>;
|
|
12
|
+
description: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TString>;
|
|
13
|
+
}>;
|
|
14
|
+
export declare const getFOTABundles: ({ apiKey, endpoint, }: {
|
|
15
|
+
apiKey: string;
|
|
16
|
+
endpoint: URL;
|
|
17
|
+
}, fetchImplementation?: typeof fetch) => () => Promise<{
|
|
18
|
+
error: Error | ValidationError;
|
|
19
|
+
} | {
|
|
20
|
+
bundles: Array<Static<typeof FOTABundle>>;
|
|
21
|
+
}>;
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { Type } from '@sinclair/typebox';
|
|
2
|
+
import { validatedFetch } from './validatedFetch.js';
|
|
3
|
+
import { FwType } from './devices.js';
|
|
4
|
+
export const FOTABundle = Type.Object({
|
|
5
|
+
bundleId: Type.String({
|
|
6
|
+
minLength: 1,
|
|
7
|
+
description: 'The ID of the bundle',
|
|
8
|
+
examples: ['APP*20a4b75a*v1.1.1-debug'],
|
|
9
|
+
}),
|
|
10
|
+
lastModified: Type.Optional(Type.String({
|
|
11
|
+
title: 'Timestamp',
|
|
12
|
+
description: 'ISO-8601 date-time string',
|
|
13
|
+
examples: ['2019-08-24T14:15:22Z'],
|
|
14
|
+
})),
|
|
15
|
+
size: Type.Number({
|
|
16
|
+
minimum: 0,
|
|
17
|
+
description: 'Size of the bundle in bytes',
|
|
18
|
+
examples: [418565],
|
|
19
|
+
}),
|
|
20
|
+
version: Type.String({ minLength: 1, examples: ['v1.1.1-debug'] }),
|
|
21
|
+
type: Type.Enum(FwType, { title: 'Firmware Type' }),
|
|
22
|
+
filenames: Type.Array(Type.String({
|
|
23
|
+
minLength: 1,
|
|
24
|
+
examples: ['hello-nrfcloud-thingy91-debug-v1.1.1-fwupd.bin'],
|
|
25
|
+
}), { description: 'The files in the bundle.' }),
|
|
26
|
+
name: Type.Optional(Type.String({
|
|
27
|
+
minLength: 1,
|
|
28
|
+
title: 'Name',
|
|
29
|
+
description: 'The name of the bundle',
|
|
30
|
+
examples: ['hello.nrfcloud.com v1.1.1-debug'],
|
|
31
|
+
})),
|
|
32
|
+
description: Type.Optional(Type.String({
|
|
33
|
+
minLength: 1,
|
|
34
|
+
title: 'Description',
|
|
35
|
+
description: 'The description of the bundle',
|
|
36
|
+
examples: ['Firmware Update Image BIN file (thingy91, debug)'],
|
|
37
|
+
})),
|
|
38
|
+
});
|
|
39
|
+
const FirmwaresType = Type.Object({
|
|
40
|
+
items: Type.Array(FOTABundle),
|
|
41
|
+
total: Type.Integer({
|
|
42
|
+
minimum: 0,
|
|
43
|
+
description: 'Reflects the total results returned by the query, which may be less than the total number of items available. If the response contains a pageNextToken value, you can supply the pageNextToken in the next request to get more results. The maximum value of total is the page limit of the request, or ten pages if no page limit is provided.',
|
|
44
|
+
}),
|
|
45
|
+
pageNextToken: Type.Optional(Type.String({
|
|
46
|
+
minLength: 1,
|
|
47
|
+
description: 'Token used to retrieve the next page of items in the list. Present in a response only if the total available results exceeds the specified limit on a page. This token does not change between requests. When supplying as a request parameter, use URL-encoding.',
|
|
48
|
+
})),
|
|
49
|
+
}, {
|
|
50
|
+
title: 'Firmware bundles',
|
|
51
|
+
description: 'Returns the list of firmware bundles. See https://api.nrfcloud.com/v1#tag/Firmware-Bundles/operation/ListFirmware',
|
|
52
|
+
});
|
|
53
|
+
export const getFOTABundles = ({ apiKey, endpoint, }, fetchImplementation) => async () => {
|
|
54
|
+
const vf = validatedFetch({
|
|
55
|
+
endpoint,
|
|
56
|
+
apiKey,
|
|
57
|
+
}, fetchImplementation);
|
|
58
|
+
return paginateFirmwares(vf);
|
|
59
|
+
};
|
|
60
|
+
const paginateFirmwares = async (vf, bundles = [], pageNextToken = undefined) => {
|
|
61
|
+
const query = new URLSearchParams({ pageLimit: '100' });
|
|
62
|
+
if (pageNextToken !== undefined)
|
|
63
|
+
query.set('pageNextToken', pageNextToken);
|
|
64
|
+
const maybeBundles = await vf({
|
|
65
|
+
resource: `firmwares`,
|
|
66
|
+
query,
|
|
67
|
+
}, FirmwaresType);
|
|
68
|
+
if ('error' in maybeBundles)
|
|
69
|
+
return maybeBundles;
|
|
70
|
+
if (maybeBundles.result.pageNextToken !== undefined)
|
|
71
|
+
return paginateFirmwares(vf, [...bundles, ...maybeBundles.result.items], maybeBundles.result.pageNextToken);
|
|
72
|
+
return { bundles: [...bundles, ...maybeBundles.result.items] };
|
|
73
|
+
};
|
package/dist/api/index.d.ts
CHANGED
package/dist/api/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hello.nrfcloud.com/nrfcloud-api-helpers",
|
|
3
|
-
"version": "5.0
|
|
3
|
+
"version": "5.1.0",
|
|
4
4
|
"description": "Helper functions for integrating nRF Cloud APIs in AWS lambdas written in TypeScript.",
|
|
5
5
|
"exports": {
|
|
6
6
|
"./*": {
|
|
@@ -32,14 +32,14 @@
|
|
|
32
32
|
"author": "Nordic Semiconductor ASA | nordicsemi.no",
|
|
33
33
|
"license": "BSD-3-Clause",
|
|
34
34
|
"devDependencies": {
|
|
35
|
-
"@bifravst/eslint-config-typescript": "6.1.
|
|
35
|
+
"@bifravst/eslint-config-typescript": "6.1.3",
|
|
36
36
|
"@bifravst/prettier-config": "1.0.2",
|
|
37
37
|
"@commitlint/config-conventional": "19.2.2",
|
|
38
38
|
"@types/aws-lambda": "8.10.138",
|
|
39
39
|
"@types/node": "20.14.2",
|
|
40
40
|
"husky": "9.0.11",
|
|
41
41
|
"tsmatchers": "5.0.2",
|
|
42
|
-
"tsx": "4.15.
|
|
42
|
+
"tsx": "4.15.2"
|
|
43
43
|
},
|
|
44
44
|
"lint-staged": {
|
|
45
45
|
"*.ts": [
|