Skip to content
This repository has been archived by the owner on Jul 9, 2024. It is now read-only.

Commit

Permalink
feat: use shared types from docs (#1339)
Browse files Browse the repository at this point in the history
- use [new protocol definitions from docs](NordicSemiconductor/asset-tracker-cloud-docs#665)
- switch to ES modules (Closes #572)
- get rid of fp-ts (makes code too complicated, no clear benefit)
  • Loading branch information
coderbyheart committed Mar 20, 2023
1 parent 2d4e58d commit ec0e3b1
Show file tree
Hide file tree
Showing 168 changed files with 1,871 additions and 4,486 deletions.
8 changes: 1 addition & 7 deletions .github/workflows/test-and-release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ jobs:
- run: echo "VERSION=${{ github.sha }}" >> $GITHUB_ENV
- name: Set up stack for End-to-End tests
run: |
npx cdk -a 'node --loader tsx --unhandled-rejections=strict cdk/cloudformation-sourcecode.ts' bootstrap aws://`aws sts get-caller-identity | jq -r '.Account' | tr -d '\n'`/${{ env.AWS_REGION }}
npx cdk -a 'node --loader tsx --unhandled-rejections=strict cdk/cloudformation-sourcecode.ts' deploy
npx cdk -a 'node --loader tsx --unhandled-rejections=strict cdk/cloudformation-test.ts' bootstrap aws://`aws sts get-caller-identity | jq -r '.Account' | tr -d '\n'`/${{ env.AWS_REGION }}
npx cdk -a 'node --loader tsx --unhandled-rejections=strict cdk/cloudformation-http-api-mock.ts' deploy '*' --require-approval never
./cli.sh configure thirdParty nrfcloud teamId `uuidgen`
openssl ecparam -name prime256v1 -genkey | ./cli.sh configure thirdParty nrfcloud agpsLocationServiceKey
Expand Down Expand Up @@ -112,11 +111,6 @@ jobs:
./cli.sh purge-buckets
npx cdk -a 'node --loader tsx --unhandled-rejections=strict cdk/cloudformation-test.ts' destroy -f '*'
npx cdk -a 'node --loader tsx --unhandled-rejections=strict cdk/cloudformation-http-api-mock.ts' destroy -f '*'
# Delete sourcecode bucket
SOURCE_CODE_BUCKET=`aws cloudformation describe-stacks --stack-name $STACK_NAME-sourcecode | jq -r '.Stacks[0].Outputs[] | select(.OutputKey == "bucketName") | .OutputValue'`
aws s3 rb s3://$SOURCE_CODE_BUCKET --force
# Delete the sourceode stack
npx cdk -a 'node --loader tsx --unhandled-rejections=strict cdk/cloudformation-sourcecode.ts' destroy -f '*'
- uses: actions/upload-artifact@v3
if: always()
with:
Expand Down
3 changes: 0 additions & 3 deletions .prettierrc.js

This file was deleted.

2 changes: 1 addition & 1 deletion agps/cacheKey.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { cacheKey } from './cacheKey'
import { cacheKey } from './cacheKey.js'

describe('cacheKey', () => {
it('should create a cache key', () =>
Expand Down
4 changes: 2 additions & 2 deletions agps/cacheKey.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Static } from '@sinclair/typebox'
import { agpsRequestSchema } from './types'
import type { Static } from '@sinclair/typebox'
import type { agpsRequestSchema } from './types.js'

export const cacheKey = ({
request,
Expand Down
53 changes: 26 additions & 27 deletions agps/deviceRequestHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@ import {
SendMessageBatchRequestEntry,
SQSClient,
} from '@aws-sdk/client-sqs'
import { Static } from '@sinclair/typebox'
import { SQSEvent, SQSMessageAttributes } from 'aws-lambda'
import { isRight } from 'fp-ts/lib/These'
import type { Static } from '@sinclair/typebox'
import type { SQSEvent, SQSMessageAttributes } from 'aws-lambda'
import { randomUUID } from 'node:crypto'
import { fromEnv } from '../util/fromEnv'
import { cacheKey } from './cacheKey'
import { AGPSDataCache, getCache } from './getCache'
import { agpsRequestSchema } from './types'
import { fromEnv } from '../util/fromEnv.js'
import { cacheKey } from './cacheKey.js'
import { AGPSDataCache, getCache } from './getCache.js'
import type { agpsRequestSchema } from './types.js'

const {
binHoursString,
Expand Down Expand Up @@ -84,7 +83,7 @@ export const handler = async (event: SQSEvent): Promise<void> => {
if (grouped[k] === undefined) {
grouped[k] = [deviceRequest]
} else {
grouped[k].push(deviceRequest)
grouped[k]?.push(deviceRequest)
}
return grouped
},
Expand All @@ -104,19 +103,10 @@ export const handler = async (event: SQSEvent): Promise<void> => {
if (resolvedRequests[cacheKey] === undefined) {
console.debug(cacheKey, 'Load from DB')
const d = await c(cacheKey)
if (isRight(d)) {
if (d.right?.unresolved !== undefined) {
console.debug(cacheKey, 'Processing of the request is finished')
resolvedRequests[cacheKey] = d.right
if (d.right.unresolved === true) {
console.error(cacheKey, `A-GPS request is unresolved.`)
return
}
}
} else {
if ('error' in d) {
console.debug(cacheKey, 'cache does not exist')
console.warn({ getCache: d.left })
const r = deviceRequests[0].request
console.warn({ getCache: d })
const r = deviceRequests[0]?.request
await Promise.all([
// Create DB entry
await dynamodb
Expand All @@ -128,25 +118,25 @@ export const handler = async (event: SQSEvent): Promise<void> => {
S: cacheKey,
},
mcc: {
N: `${r.mcc}`,
N: `${r?.mcc}`,
},
mnc: {
N: `${r.mnc}`,
N: `${r?.mnc}`,
},
cell: {
N: `${r.cell}`,
N: `${r?.cell}`,
},
area: {
N: `${r.area}`,
N: `${r?.area}`,
},
phycell:
r.phycell !== undefined
r?.phycell !== undefined
? {
N: `${r.phycell}`,
}
: { NULL: true },
types: {
NS: r.types.map((t) => `${t}`),
NS: r?.types.map((t) => `${t}`) ?? [],
},
updatedAt: {
S: new Date().toISOString(),
Expand Down Expand Up @@ -189,13 +179,22 @@ export const handler = async (event: SQSEvent): Promise<void> => {
)
}),
])
} else {
if (d.unresolved !== undefined) {
console.debug(cacheKey, 'Processing of the request is finished')
resolvedRequests[cacheKey] = d
if (d.unresolved === true) {
console.error(cacheKey, `A-GPS request is unresolved.`)
return
}
}
}
}

// The data for these requests is available
if (
resolvedRequests[cacheKey]?.unresolved !== undefined &&
resolvedRequests[cacheKey].unresolved === false
resolvedRequests[cacheKey]?.unresolved === false
) {
console.debug(cacheKey, 'data for these requests is available')
console.debug(
Expand Down
33 changes: 17 additions & 16 deletions agps/getCache.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { DynamoDBClient, GetItemCommand } from '@aws-sdk/client-dynamodb'
import { unmarshall } from '@aws-sdk/util-dynamodb'
import { Static } from '@sinclair/typebox'
import { Either, left, right } from 'fp-ts/lib/Either'
import { ErrorInfo, ErrorType } from '../api/ErrorInfo'
import { agpsRequestSchema } from './types'
import type { Static } from '@sinclair/typebox'
import { ErrorInfo, ErrorType } from '../api/ErrorInfo.js'
import type { agpsRequestSchema } from './types.js'

export type AGPSDataCache = Static<typeof agpsRequestSchema> & {
source: string
Expand All @@ -14,7 +13,7 @@ export type AGPSDataCache = Static<typeof agpsRequestSchema> & {

export const getCache =
({ dynamodb, TableName }: { dynamodb: DynamoDBClient; TableName: string }) =>
async (cacheKey: string): Promise<Either<ErrorInfo, AGPSDataCache>> => {
async (cacheKey: string): Promise<{ error: ErrorInfo } | AGPSDataCache> => {
try {
const { Item } = await dynamodb.send(
new GetItemCommand({
Expand All @@ -30,7 +29,7 @@ export const getCache =
if (Item === undefined) throw new Error('NOT_FOUND')

const entry = unmarshall(Item)
const i = {
return {
...entry,
updatedAt: new Date(entry.updatedAt as string),
types: [...(entry.types as Set<number>)],
Expand All @@ -39,17 +38,17 @@ export const getCache =
? [...(entry.dataHex as Set<string>)]
: undefined,
} as AGPSDataCache

return right(i)
} catch (err) {
if (
(err as Error).message === 'NOT_FOUND' ||
(err as Error).name === 'ResourceNotFoundException'
)
return left({
type: ErrorType.EntityNotFound,
message: `Report ${cacheKey} not found!`,
})
return {
error: {
type: ErrorType.EntityNotFound,
message: `Report ${cacheKey} not found!`,
},
}
console.error(
JSON.stringify({
getCache: {
Expand All @@ -60,9 +59,11 @@ export const getCache =
},
}),
)
return left({
type: ErrorType.InternalError,
message: (err as Error).message,
})
return {
error: {
type: ErrorType.InternalError,
message: (err as Error).message,
},
}
}
}
3 changes: 3 additions & 0 deletions agps/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ export enum AGPSType {

const PositiveInteger = Type.Integer({ minimum: 1, title: 'positive integer' })

/**
* @see https://api.nrfcloud.com/v1#tag/Assisted-GPS/operation/GetAssistanceData
*/
export const agpsRequestSchema = Type.Object({
mcc: Type.Integer({ minimum: 100, maximum: 999 }),
mnc: Type.Integer({ minimum: 0, maximum: 999 }),
Expand Down
2 changes: 1 addition & 1 deletion api/res.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { APIGatewayProxyResultV2 } from 'aws-lambda'
import type { APIGatewayProxyResultV2 } from 'aws-lambda'

export const res =
(statusCode: number, options?: { expires: number }) =>
Expand Down
24 changes: 0 additions & 24 deletions api/resFP.ts

This file was deleted.

26 changes: 0 additions & 26 deletions api/validateWithJSONSchema.spec.ts

This file was deleted.

29 changes: 0 additions & 29 deletions api/validateWithJSONSchema.ts

This file was deleted.

30 changes: 0 additions & 30 deletions api/validateWithJSONSchemaFP.spec.ts

This file was deleted.

31 changes: 0 additions & 31 deletions api/validateWithJSONSchemaFP.ts

This file was deleted.

Loading

0 comments on commit ec0e3b1

Please sign in to comment.