From 1929c95f92f0fbd4a77c25f2ed6cc4bb106c054e Mon Sep 17 00:00:00 2001 From: Taylor Beseda Date: Fri, 2 Feb 2024 11:02:17 -0700 Subject: [PATCH] Update types imported from aws-sdk (#555) * remove types imported from aws-sdk * use types from aws-lite/-types * correct tables._client type also arc.ws._api returns client.ApiGatewayManagementApi also remove some old JSDoc since it's mixing with .d.ts in a confusing way for consumers * include types test in main test command --- .eslintignore | 1 + package.json | 6 +++++- src/events/index.js | 23 +---------------------- src/static/index.js | 2 +- src/ws/index.js | 43 ++----------------------------------------- types/events.d.ts | 7 ++++--- types/http.d.ts | 2 +- types/index.d.ts | 12 ++++++------ types/index.test-d.ts | 22 ++++++++++++++-------- types/tables.d.ts | 23 ++++++++++++----------- types/ws.d.ts | 11 ++++++----- 11 files changed, 53 insertions(+), 99 deletions(-) diff --git a/.eslintignore b/.eslintignore index 1083980c..f6fbb297 100644 --- a/.eslintignore +++ b/.eslintignore @@ -4,3 +4,4 @@ coverage/ dist.js scratch/ src/http/get-index +types/ diff --git a/package.json b/package.json index efeda135..049da993 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,7 @@ "test:unit": "cross-env tape 'test/unit/**/*-test.js' | tap-arc", "test:integration": "cross-env tape 'test/integration/**/*-test.js' | tap-arc", "coverage": "nyc --reporter=lcov --reporter=text npm run test:unit", - "test": "npm run lint && npm run test:integration && npm run coverage", + "test": "npm run lint && npm run test:integration && npm run coverage && npm run test:types", "test:types": "tsd --files types/*.test-d.ts", "rc": "npm version prerelease --preid RC" }, @@ -44,6 +44,10 @@ "@architect/eslint-config": "2.1.1", "@architect/req-res-fixtures": "git+https://github.com/architect/req-res-fixtures.git", "@architect/sandbox": "^6.0.0-RC.1", + "@aws-lite/apigatewaymanagementapi-types": "^0.0.9", + "@aws-lite/dynamodb-types": "^0.3.4", + "@aws-lite/sns-types": "^0.0.5", + "@aws-lite/sqs-types": "^0.2.1", "@types/aws-lambda": "^8.10.133", "@types/node": "18", "cross-env": "~7.0.3", diff --git a/src/events/index.js b/src/events/index.js index c949ea89..c3c2e48d 100644 --- a/src/events/index.js +++ b/src/events/index.js @@ -4,26 +4,5 @@ let subFactory = require('./subscribe') module.exports = function eventsAndQueuesFactory (arc, type) { let publish = pubFactory(arc, type) let subscribe = subFactory(type) - return { - /** - * `arc.events|queues.publish` - * publish events and queues - * - * @param {Object} params - * @param {String} params.name - the event name (required) - * @param {Object} params.payload - a json event payload (required) - * @param {Function} callback - a node style errback (optional) - * @returns {Promise} - returned if no callback is supplied - */ - publish, - - /** - * `arc.events|queues.subscribe` - * listen for events and queues - * - * @param {Function} handler - a single event handler function - * @returns {Lambda} - a Lambda function sig - */ - subscribe - } + return { publish, subscribe } } diff --git a/src/static/index.js b/src/static/index.js index 7e57bc87..e25387e2 100644 --- a/src/static/index.js +++ b/src/static/index.js @@ -8,7 +8,7 @@ let { join } = require('path') * In order to keep this method sync, it does not use reflection to get fingerprint status * - Not checking @static fingerprint true (which we used to read from the .arc file) is possibly dangerous, so ensure asset path is valid * - ? TODO: add fingerprint state to env vars in Arc 6 to restore config safety? - * @param {string} path - the path to the asset (eg. /index.js) + * @param {string} asset - the path to the asset (eg. /index.js) * @returns {string} path - the resolved asset path (eg. /_static/index-xxx.js) */ module.exports = function _static (asset, options = {}) { diff --git a/src/ws/index.js b/src/ws/index.js index 32bb67ab..80be0b52 100644 --- a/src/ws/index.js +++ b/src/ws/index.js @@ -28,37 +28,18 @@ function instantiateAPI () { }) } -/** - * arc.ws._api - * - * Get the raw WebSocket client - * - * @param {Function} callback - a node style errback (optional) - * @returns {Promise} - returned if no callback is supplied - */ function _api (callback) { if (callback) instantiateAPI() - .then(client => callback(null, client)) + .then(client => callback(null, client.ApiGatewayManagementApi)) .catch(callback) else return new Promise((res, rej) => { instantiateAPI() - .then(client => res(client)) + .then(client => res(client.ApiGatewayManagementApi)) .catch(rej) }) } -/** - * arc.ws.send - * - * Publish WebSocket events - * - * @param {Object} params - * @param {String} params.id - the ws connection id (required) - * @param {Object} params.payload - an event payload (required) - * @param {Function} callback - a node style errback (optional) - * @returns {Promise} - returned if no callback is supplied - */ function send ({ id, payload }, callback) { if (callback) instantiateAPI() .then(client => { @@ -87,16 +68,6 @@ function send ({ id, payload }, callback) { }) } -/** - * arc.ws.close - * - * Terminate a WebSocket client connection - * - * @param {Object} params - * @param {String} params.id - the ws connection id (required) - * @param {Function} callback - a node style errback (optional) - * @returns {Promise} - returned if no callback is supplied - */ function close ({ id }, callback) { if (callback) instantiateAPI() .then(client => { @@ -123,16 +94,6 @@ function close ({ id }, callback) { }) } -/** - * arc.ws.info - * - * Get info on a WebSocket client connection - * - * @param {Object} params - * @param {String} params.id - the ws connection id (required) - * @param {Function} callback - a node style errback (optional) - * @returns {Promise} - returned if no callback is supplied - */ function info ({ id }, callback) { if (callback) instantiateAPI() .then(client => { diff --git a/types/events.d.ts b/types/events.d.ts index 62a0f1ba..5c5fada3 100644 --- a/types/events.d.ts +++ b/types/events.d.ts @@ -1,5 +1,6 @@ -import { SNS, SQS } from "aws-sdk"; import { Callback } from "./util"; +import type { PublishResponse as SnsPublishResponse } from "@aws-lite/sns-types" +import type { SendMessageResponse as SqsPublishResponse } from "@aws-lite/sqs-types" // Turn off automatic exporting export { }; @@ -26,5 +27,5 @@ interface EventsOrQueues { ): LambdaFunction; } -export type ArcEvents = EventsOrQueues; -export type ArcQueues = EventsOrQueues; +export type ArcEvents = EventsOrQueues; +export type ArcQueues = EventsOrQueues; diff --git a/types/http.d.ts b/types/http.d.ts index f3f2f3a2..473f085d 100644 --- a/types/http.d.ts +++ b/types/http.d.ts @@ -2,7 +2,7 @@ import { APIGatewayProxyEvent, Context, APIGatewayProxyResult, -} from "aws-lambda"; +} from "aws-lambda"; // from @types/aws-lambda import { Callback } from "./util"; // Turn off automatic exporting diff --git a/types/index.d.ts b/types/index.d.ts index 88ddfe6b..21131dca 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -1,18 +1,18 @@ /// +import { ArcEvents, ArcQueues } from "./events"; import { ArcHTTP, HttpHandler, HttpAsyncHandler } from "./http"; import { ArcStatic } from "./static"; -import { ArcWebSocket } from "./ws"; -import { ArcEvents, ArcQueues } from "./events"; import { ArcTables } from "./tables"; +import { ArcWebSocket } from "./ws"; export type { HttpHandler, HttpAsyncHandler }; export type ArcServices = () => Promise>; -export const http: ArcHTTP; -export const static: ArcStatic; -export const ws: ArcWebSocket; -export const services: ArcServices; export const events: ArcEvents; +export const http: ArcHTTP; export const queues: ArcQueues; +export const services: ArcServices; +export const static: ArcStatic; export const tables: ArcTables; +export const ws: ArcWebSocket; diff --git a/types/index.test-d.ts b/types/index.test-d.ts index 0449c6ac..7e5b8e57 100644 --- a/types/index.test-d.ts +++ b/types/index.test-d.ts @@ -1,19 +1,26 @@ -import { ApiGatewayManagementApi, DynamoDB, SNS, SQS } from "aws-sdk"; -import { Context } from "aws-lambda"; +import type { AwsLiteClient } from "@aws-lite/client" +import type { GetConnectionResponse } from "@aws-lite/apigatewaymanagementapi-types"; +import type { PublishResponse } from "@aws-lite/sns-types" +import type { SendMessageResponse } from "@aws-lite/sqs-types" +import type { Context } from "aws-lambda"; import { expectType, expectAssignable, expectNotAssignable } from "tsd"; import arc from "../"; import type { HttpHandler, HttpAsyncHandler } from "../" import type { HttpMethods, HttpRequest, HttpResponse } from "./http"; +// SERVICES +const servicesResult = await arc.services(); +expectType>(servicesResult); + // EVENTS const eventsPublishArg = { name: "test", payload: { foo: "bar" } }; const eventsPublishResult = await arc.events.publish(eventsPublishArg); -expectType(eventsPublishResult); +expectType(eventsPublishResult); // QUEUES const queuesPublishArg = { name: "test", payload: { foo: "bar" } }; const queuesPublishResult = await arc.queues.publish(queuesPublishArg); -expectType(queuesPublishResult); +expectType(queuesPublishResult); // HTTP const middleware: HttpHandler = (req, res, next) => { @@ -94,8 +101,7 @@ arc.static("/", { stagePath: false }); // TABLES const dbClient = await arc.tables() -expectType(dbClient._db) -expectType(dbClient._doc) +expectType(dbClient._client) expectType(dbClient.name('widgets')) expectType>(dbClient.reflect()) const myTable = dbClient.foobar @@ -122,9 +128,9 @@ await myTable.scanAll({ }) // WS -expectType(arc.ws._api); +expectType(await arc.ws._api()); expectType(await arc.ws.send({ id: "foo", payload: { bar: "baz" } })); expectType(await arc.ws.close({ id: "foo" })); -expectType( +expectType( await arc.ws.info({ id: "foo" }), ); diff --git a/types/tables.d.ts b/types/tables.d.ts index 2eed8247..2bf50b96 100644 --- a/types/tables.d.ts +++ b/types/tables.d.ts @@ -1,4 +1,5 @@ -import type { DynamoDB } from "aws-sdk"; +import type { AwsLiteClient } from "@aws-lite/client" +import type { QueryResponse, ScanResponse, UpdateItemResponse } from "@aws-lite/dynamodb-types" import { Callback } from "./util"; // Turn off automatic exporting @@ -17,17 +18,16 @@ type ItemsOutput = Omit & { Items: Item[]; }; -type QueryParams = Params; -type QueryOutput = ItemsOutput; +type QueryParams = Params[0]>; +type QueryOutput = ItemsOutput; -type ScanParams = Params; -type ScanOutput = ItemsOutput; +type ScanParams = Params[0]>; +type ScanOutput = ItemsOutput; type UpdateParams = ParamsWithKey< - DynamoDB.DocumentClient.UpdateItemInput, + Parameters[0], Item >; -type UpdateOutput = DynamoDB.DocumentClient.UpdateItemOutput; // Depending on the operation, the key attributes may be mandatory, but we don't // know what the key attributes are, so Partial is the best we can do. @@ -51,8 +51,8 @@ export interface ArcTable { scanAll(params: ScanParams): Promise; - update(params: UpdateParams): Promise; - update(params: UpdateParams, callback: Callback): void; + update(params: UpdateParams): Promise; + update(params: UpdateParams, callback: Callback): void; } type ArcDBWith = { @@ -64,8 +64,9 @@ export type ArcDB = ArcDBWith & { reflect(): { [tableName in keyof Tables]: string; }; - _db: DynamoDB; - _doc: DynamoDB.DocumentClient; + _client: AwsLiteClient["DynamoDB"]; + // _db: DynamoDB; + // _doc: DynamoDB.DocumentClient; }; // Permissive by default: allows any table, any inputs, any outputs. diff --git a/types/ws.d.ts b/types/ws.d.ts index a5f38e59..bef3fdb9 100644 --- a/types/ws.d.ts +++ b/types/ws.d.ts @@ -1,4 +1,5 @@ -import { ApiGatewayManagementApi } from "aws-sdk"; +import type { AwsLiteClient } from "@aws-lite/client" +import type { GetConnectionResponse } from "@aws-lite/apigatewaymanagementapi-types"; import { Callback } from "./util"; // Turn off automatic exporting @@ -7,10 +8,10 @@ export { }; type SendParams = { id: string; payload: any }; type CloseParams = { id: string }; type InfoParams = { id: string }; -type InfoResponse = ApiGatewayManagementApi.Types.GetConnectionResponse; export interface ArcWebSocket { - _api: ApiGatewayManagementApi; + _api(): Promise; + _api(callback: Callback): void; send(params: SendParams): Promise; send(params: SendParams, callback: Callback): void; @@ -18,6 +19,6 @@ export interface ArcWebSocket { close(params: CloseParams): Promise; close(params: CloseParams, callback: Callback): void; - info(params: InfoParams): Promise; - info(params: InfoParams, callback: Callback): void; + info(params: InfoParams): Promise; + info(params: InfoParams, callback: Callback): void; }