Skip to content

Commit c2f63cd

Browse files
committed
feat(api): add more implementations
1 parent 58c88c9 commit c2f63cd

File tree

5 files changed

+134
-1
lines changed

5 files changed

+134
-1
lines changed

src/api/bulkOps.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { Type, type Static } from '@sinclair/typebox'
2+
import { ValidationError, validatedFetch } from './validatedFetch.js'
3+
4+
/**
5+
* @link https://api.nrfcloud.com/v1/#tag/Bulk-Ops-Requests/operation/FetchBulkOpsRequest
6+
*/
7+
export const BulkOpsRequestType = Type.Object({
8+
bulkOpsRequestId: Type.String(), // e.g. '01EZZJVDQJPWT7V4FWNVDHNMM5'
9+
status: Type.Union([
10+
Type.Literal('PENDING'),
11+
Type.Literal('IN_PROGRESS'),
12+
Type.Literal('FAILED'),
13+
Type.Literal('SUCCEEDED'),
14+
]),
15+
})
16+
17+
export const bulkOpsRequests =
18+
(
19+
{
20+
apiKey,
21+
endpoint,
22+
}: {
23+
apiKey: string
24+
endpoint: URL
25+
},
26+
fetchImplementation?: typeof fetch,
27+
) =>
28+
async (
29+
bulkOpsId: string,
30+
): Promise<
31+
| { error: Error | ValidationError }
32+
| { result: Static<typeof BulkOpsRequestType> }
33+
> => {
34+
const vf = validatedFetch({ endpoint, apiKey }, fetchImplementation)
35+
return vf(
36+
{
37+
resource: `bulk-ops-requests/${encodeURIComponent(bulkOpsId)}`,
38+
},
39+
BulkOpsRequestType,
40+
)
41+
}

src/api/groundFix.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import {
2+
accuracy as TAccuracy,
3+
lat as TLat,
4+
lng as TLng,
5+
} from '@hello.nrfcloud.com/proto/hello'
6+
import { Type, type Static } from '@sinclair/typebox'
7+
import {
8+
JSONPayload,
9+
ValidationError,
10+
validatedFetch,
11+
} from './validatedFetch.js'
12+
13+
/**
14+
* @link https://api.nrfcloud.com/v1/#tag/Ground-Fix
15+
*/
16+
export const GroundFixType = Type.Object({
17+
lat: TLat, // 63.41999531
18+
lon: TLng, // 10.42999506
19+
uncertainty: TAccuracy, // 2420
20+
fulfilledWith: Type.Literal('SCELL'),
21+
})
22+
23+
export const groundFix =
24+
(
25+
{
26+
apiKey,
27+
endpoint,
28+
}: {
29+
apiKey: string
30+
endpoint: URL
31+
},
32+
fetchImplementation?: typeof fetch,
33+
) =>
34+
async (cell: {
35+
mcc: string
36+
mnc: string
37+
eci: number
38+
tac: number
39+
rsrp: number
40+
}): Promise<
41+
| { error: Error | ValidationError }
42+
| { result: Static<typeof GroundFixType> }
43+
> => {
44+
const vf = validatedFetch({ endpoint, apiKey }, fetchImplementation)
45+
return vf(
46+
{
47+
resource: 'location/ground-fix',
48+
payload: JSONPayload({
49+
lte: [cell],
50+
}),
51+
},
52+
GroundFixType,
53+
)
54+
}

src/api/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,6 @@ export * from './deleteAccountDevice.js'
88
export * from './getDeviceShadow.js'
99
export * from './DeviceShadow.js'
1010
export * from './updateDeviceShadow.js'
11+
export * from './serviceToken.js'
12+
export * from './groundFix.js'
13+
export * from './bulkOps.js'

src/api/serviceToken.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { Type } from '@sinclair/typebox'
2+
import { validatedFetch } from './validatedFetch.js'
3+
4+
/**
5+
* @link https://api.nrfcloud.com/v1/#tag/Account/operation/GetServiceToken
6+
*/
7+
export const ServiceToken = Type.Object({
8+
token: Type.String(),
9+
})
10+
11+
export const serviceToken =
12+
(
13+
fetchImplementation?: typeof fetch,
14+
onError?: (error: Error) => void,
15+
): ((args: { apiEndpoint: URL; apiKey: string }) => Promise<string>) =>
16+
async ({ apiEndpoint, apiKey }) => {
17+
const vf = validatedFetch(
18+
{ endpoint: apiEndpoint, apiKey },
19+
fetchImplementation,
20+
)
21+
const maybeResult = await vf(
22+
{ resource: 'account/service-token' },
23+
ServiceToken,
24+
)
25+
26+
if ('error' in maybeResult) {
27+
onError?.(maybeResult.error)
28+
throw maybeResult.error
29+
}
30+
31+
return maybeResult.result.token
32+
}

tsconfig.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515
"noUncheckedIndexedAccess": true,
1616
"noUnusedLocals": true,
1717
"noEmit": true,
18-
"verbatimModuleSyntax": true
18+
"verbatimModuleSyntax": true,
19+
"baseUrl": ".",
20+
"rootDir": "."
1921
},
22+
"include": ["src/**/*"],
2023
"exclude": ["dist/*"]
2124
}

0 commit comments

Comments
 (0)