-
Notifications
You must be signed in to change notification settings - Fork 1
feat: validate nRF Cloud API response #305
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
coderbyheart
merged 5 commits into
saga
from
291-implement-nrf-cloud-api-response-validation
Oct 12, 2023
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6856831
feat: validate nRF Cloud API response
pudkrong 57f6f00
feat: reuse validatedFetch
pudkrong 795db45
fix: remove empty file
coderbyheart 1be627c
refactor: use higher-level API for validatedFetch
coderbyheart ef5e92c
refactor: improve DeviceShadow type and merge with existing type
coderbyheart File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { validateWithTypeBox } from '@hello.nrfcloud.com/proto' | ||
import { DeviceShadow } from './DeviceShadow.js' | ||
|
||
describe('DeviceShadow type', () => { | ||
it('should document the device shadow object', () => { | ||
const res = validateWithTypeBox(DeviceShadow)({ | ||
id: 'some-device', | ||
state: { | ||
version: 42, | ||
reported: { | ||
dev: { | ||
v: { | ||
imei: '358299840016535', | ||
iccid: '89450421180216254864', | ||
modV: 'mfw_nrf91x1_2.0.0-77.beta', | ||
brdV: 'thingy91x_nrf9161', | ||
appV: '0.0.0-development', | ||
}, | ||
ts: 1697102116821, | ||
}, | ||
}, | ||
metadata: { | ||
reported: { | ||
dev: { | ||
v: { | ||
imei: { | ||
timestamp: 1697102122, | ||
}, | ||
iccid: { | ||
timestamp: 1697102122, | ||
}, | ||
modV: { | ||
timestamp: 1697102122, | ||
}, | ||
brdV: { | ||
timestamp: 1697102122, | ||
}, | ||
appV: { | ||
timestamp: 1697102122, | ||
}, | ||
}, | ||
ts: { | ||
timestamp: 1697102122, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}) | ||
expect(res).not.toHaveProperty('errors') | ||
}) | ||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,22 @@ | ||
export type DeviceShadow = { | ||
id: string | ||
state: { | ||
reported: Record<string, any> | ||
version: number | ||
metadata: Record<string, any> | ||
} | ||
} | ||
import { Type, type Static } from '@sinclair/typebox' | ||
|
||
const PropertyMetadata = Type.Union([ | ||
Type.Object({ timestamp: Type.Integer({ minimum: 1, maximum: 9999999999 }) }), | ||
Type.Record(Type.String({ minLength: 1 }), Type.Unknown()), | ||
]) | ||
|
||
/** | ||
* @link https://api.nrfcloud.com/v1/#tag/All-Devices/operation/ListDevices | ||
*/ | ||
export const DeviceShadow = Type.Object({ | ||
id: Type.String(), | ||
state: Type.Object({ | ||
reported: Type.Object({}), | ||
version: Type.Number(), | ||
metadata: Type.Object({ | ||
reported: Type.Record(Type.String({ minLength: 1 }), PropertyMetadata), | ||
}), | ||
}), | ||
}) | ||
|
||
export type DeviceShadowType = Static<typeof DeviceShadow> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,33 @@ | ||
import { slashless } from '../util/slashless.js' | ||
import { Type, type Static } from '@sinclair/typebox' | ||
import { validatedFetch } from './validatedFetch.js' | ||
|
||
export type CertificateCredentials = { | ||
clientCert: string | ||
privateKey: string | ||
} | ||
/** | ||
* @link https://api.nrfcloud.com/v1/#tag/Account-Devices/operation/CreateAccountDevice | ||
*/ | ||
const CertificateCredentials = Type.Object({ | ||
clientCert: Type.String(), | ||
privateKey: Type.String(), | ||
}) | ||
|
||
export const createAccountDevice = async ({ | ||
apiKey, | ||
endpoint, | ||
}: { | ||
apiKey: string | ||
endpoint: URL | ||
}): Promise<CertificateCredentials> => { | ||
const accountDevice = await // FIXME: validate response | ||
( | ||
await fetch(`${slashless(endpoint)}/v1/devices/account`, { | ||
method: 'POST', | ||
headers: { | ||
Authorization: `Bearer ${apiKey}`, | ||
}, | ||
}) | ||
).json() | ||
}): Promise<Static<typeof CertificateCredentials>> => { | ||
const vf = validatedFetch({ endpoint, apiKey }) | ||
const maybeResult = await vf( | ||
{ resource: 'devices/account', method: 'POST' }, | ||
CertificateCredentials, | ||
) | ||
|
||
if ('error' in maybeResult) { | ||
throw maybeResult.error | ||
} | ||
|
||
return { | ||
clientCert: accountDevice.clientCert, | ||
privateKey: accountDevice.privateKey, | ||
clientCert: maybeResult.result.clientCert, | ||
privateKey: maybeResult.result.privateKey, | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is required to allow
loggingFetch
to be able to pass asfetchImplementation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've addressed this in a refactor.