Skip to content

Commit c1ee4cb

Browse files
committed
refactor: use typedFetch
1 parent 360d913 commit c1ee4cb

File tree

4 files changed

+27
-126
lines changed

4 files changed

+27
-126
lines changed

hello/api.ts

Lines changed: 22 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
1-
import { models } from '@hello.nrfcloud.com/proto-map'
2-
import { DeviceId } from '@hello.nrfcloud.com/proto-map/api'
3-
import type { ProblemDetail } from '@hello.nrfcloud.com/proto/hello'
4-
import { Type, type Static } from '@sinclair/typebox'
5-
import { typedFetch } from './typedFetch.js'
6-
7-
const Model = Type.Union(
8-
Object.keys(models).map((model) => Type.Literal(model)),
9-
)
1+
import {
2+
DeviceIdentity,
3+
typedFetch,
4+
type TypedFetchResponse,
5+
} from '@hello.nrfcloud.com/proto/hello'
106

117
export const helloApi = ({
128
endpoint,
@@ -15,29 +11,21 @@ export const helloApi = ({
1511
endpoint: URL
1612
fetchImplementation?: typeof fetch
1713
}): {
18-
getDeviceByFingerprint: (fingerprint: string) => Promise<
19-
| {
20-
error: Omit<Static<typeof ProblemDetail>, '@context'>
21-
}
22-
| {
23-
result: {
24-
id: string
25-
model: Static<typeof Model>
26-
}
27-
}
28-
>
29-
} => ({
30-
getDeviceByFingerprint: async (fingerprint: string) =>
31-
typedFetch({
32-
url: new URL(
33-
`./device?${new URLSearchParams({ fingerprint }).toString()}`,
34-
endpoint,
14+
getDeviceByFingerprint: (
15+
fingerprint: string,
16+
) => Promise<TypedFetchResponse<typeof DeviceIdentity>>
17+
} => {
18+
const getDeviceByFingerprintRequest = typedFetch({
19+
responseBodySchema: DeviceIdentity,
20+
fetchImplementation,
21+
})
22+
return {
23+
getDeviceByFingerprint: async (fingerprint: string) =>
24+
getDeviceByFingerprintRequest(
25+
new URL(
26+
`./device?${new URLSearchParams({ fingerprint }).toString()}`,
27+
endpoint,
28+
),
3529
),
36-
RequestBodySchema: Type.Undefined(),
37-
ResponseBodySchema: Type.Object({
38-
id: DeviceId,
39-
model: Model,
40-
}),
41-
fetchImplementation,
42-
})(),
43-
})
30+
}
31+
}

hello/typedFetch.ts

Lines changed: 0 additions & 87 deletions
This file was deleted.

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@
102102
"@hello.nrfcloud.com/certificate-helpers": "1.0.0",
103103
"@hello.nrfcloud.com/lambda-helpers": "1.0.2",
104104
"@hello.nrfcloud.com/nrfcloud-api-helpers": "2.0.0",
105-
"@hello.nrfcloud.com/proto": "6.5.3",
105+
"@hello.nrfcloud.com/proto": "6.6.0",
106106
"@hello.nrfcloud.com/proto-map": "5.3.0",
107107
"@middy/core": "5.3.2",
108108
"@nordicsemiconductor/from-env": "3.0.1",

0 commit comments

Comments
 (0)