Skip to content

Commit

Permalink
Update types imported from aws-sdk (#555)
Browse files Browse the repository at this point in the history
* remove types imported from aws-sdk

* use types from aws-lite/<plugin>-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
  • Loading branch information
tbeseda authored Feb 2, 2024
1 parent 653db73 commit 1929c95
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 99 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ coverage/
dist.js
scratch/
src/http/get-index
types/
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down Expand Up @@ -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",
Expand Down
23 changes: 1 addition & 22 deletions src/events/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}
2 changes: 1 addition & 1 deletion src/static/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}) {
Expand Down
43 changes: 2 additions & 41 deletions src/ws/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down Expand Up @@ -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 => {
Expand All @@ -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 => {
Expand Down
7 changes: 4 additions & 3 deletions types/events.d.ts
Original file line number Diff line number Diff line change
@@ -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 { };
Expand All @@ -26,5 +27,5 @@ interface EventsOrQueues<PublishResult> {
): LambdaFunction;
}

export type ArcEvents = EventsOrQueues<SNS.Types.PublishResponse>;
export type ArcQueues = EventsOrQueues<SQS.Types.SendMessageResult>;
export type ArcEvents = EventsOrQueues<SnsPublishResponse>;
export type ArcQueues = EventsOrQueues<SqsPublishResponse>;
2 changes: 1 addition & 1 deletion types/http.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
/// <reference types="node" />

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<Record<string, any>>;

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;
22 changes: 14 additions & 8 deletions types/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -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<Record<string, any>>(servicesResult);

// EVENTS
const eventsPublishArg = { name: "test", payload: { foo: "bar" } };
const eventsPublishResult = await arc.events.publish(eventsPublishArg);
expectType<SNS.Types.PublishResponse>(eventsPublishResult);
expectType<PublishResponse>(eventsPublishResult);

// QUEUES
const queuesPublishArg = { name: "test", payload: { foo: "bar" } };
const queuesPublishResult = await arc.queues.publish(queuesPublishArg);
expectType<SQS.Types.SendMessageResult>(queuesPublishResult);
expectType<SendMessageResponse>(queuesPublishResult);

// HTTP
const middleware: HttpHandler = (req, res, next) => {
Expand Down Expand Up @@ -94,8 +101,7 @@ arc.static("/", { stagePath: false });

// TABLES
const dbClient = await arc.tables()
expectType<DynamoDB>(dbClient._db)
expectType<DynamoDB.DocumentClient>(dbClient._doc)
expectType<AwsLiteClient["DynamoDB"]>(dbClient._client)
expectType<string>(dbClient.name('widgets'))
expectType<Record<string, string>>(dbClient.reflect())
const myTable = dbClient.foobar
Expand All @@ -122,9 +128,9 @@ await myTable.scanAll({
})

// WS
expectType<ApiGatewayManagementApi>(arc.ws._api);
expectType<AwsLiteClient["ApiGatewayManagementApi"]>(await arc.ws._api());
expectType<void>(await arc.ws.send({ id: "foo", payload: { bar: "baz" } }));
expectType<void>(await arc.ws.close({ id: "foo" }));
expectType<ApiGatewayManagementApi.Types.GetConnectionResponse>(
expectType<GetConnectionResponse>(
await arc.ws.info({ id: "foo" }),
);
23 changes: 12 additions & 11 deletions types/tables.d.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -17,17 +18,16 @@ type ItemsOutput<OutputType, Item> = Omit<OutputType, "Items"> & {
Items: Item[];
};

type QueryParams = Params<DynamoDB.DocumentClient.QueryInput>;
type QueryOutput<Item> = ItemsOutput<DynamoDB.DocumentClient.QueryOutput, Item>;
type QueryParams = Params<Parameters<AwsLiteClient["DynamoDB"]["Query"]>[0]>;
type QueryOutput<Item> = ItemsOutput<QueryResponse, Item>;

type ScanParams = Params<DynamoDB.DocumentClient.ScanInput>;
type ScanOutput<Item> = ItemsOutput<DynamoDB.DocumentClient.ScanOutput, Item>;
type ScanParams = Params<Parameters<AwsLiteClient["DynamoDB"]["Scan"]>[0]>;
type ScanOutput<Item> = ItemsOutput<ScanResponse, Item>;

type UpdateParams<Item> = ParamsWithKey<
DynamoDB.DocumentClient.UpdateItemInput,
Parameters<AwsLiteClient["DynamoDB"]["UpdateItem"]>[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.
Expand All @@ -51,8 +51,8 @@ export interface ArcTable<Item = unknown> {

scanAll(params: ScanParams): Promise<Item[]>;

update(params: UpdateParams<Item>): Promise<UpdateOutput>;
update(params: UpdateParams<Item>, callback: Callback<UpdateOutput>): void;
update(params: UpdateParams<Item>): Promise<UpdateItemResponse>;
update(params: UpdateParams<Item>, callback: Callback<UpdateItemResponse>): void;
}

type ArcDBWith<Tables> = {
Expand All @@ -64,8 +64,9 @@ export type ArcDB<Tables> = ArcDBWith<Tables> & {
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.
Expand Down
11 changes: 6 additions & 5 deletions types/ws.d.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -7,17 +8,17 @@ 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<AwsLiteClient["ApiGatewayManagementApi"]>;
_api(callback: Callback<AwsLiteClient["ApiGatewayManagementApi"]>): void;

send(params: SendParams): Promise<void>;
send(params: SendParams, callback: Callback<void>): void;

close(params: CloseParams): Promise<void>;
close(params: CloseParams, callback: Callback<void>): void;

info(params: InfoParams): Promise<InfoResponse>;
info(params: InfoParams, callback: Callback<InfoResponse>): void;
info(params: InfoParams): Promise<GetConnectionResponse>;
info(params: InfoParams, callback: Callback<GetConnectionResponse>): void;
}

0 comments on commit 1929c95

Please sign in to comment.