Skip to content

Commit 1929c95

Browse files
authored
Update types imported from aws-sdk (#555)
* 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
1 parent 653db73 commit 1929c95

File tree

11 files changed

+53
-99
lines changed

11 files changed

+53
-99
lines changed

.eslintignore

+1
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ coverage/
44
dist.js
55
scratch/
66
src/http/get-index
7+
types/

package.json

+5-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"test:unit": "cross-env tape 'test/unit/**/*-test.js' | tap-arc",
1616
"test:integration": "cross-env tape 'test/integration/**/*-test.js' | tap-arc",
1717
"coverage": "nyc --reporter=lcov --reporter=text npm run test:unit",
18-
"test": "npm run lint && npm run test:integration && npm run coverage",
18+
"test": "npm run lint && npm run test:integration && npm run coverage && npm run test:types",
1919
"test:types": "tsd --files types/*.test-d.ts",
2020
"rc": "npm version prerelease --preid RC"
2121
},
@@ -44,6 +44,10 @@
4444
"@architect/eslint-config": "2.1.1",
4545
"@architect/req-res-fixtures": "git+https://github.com/architect/req-res-fixtures.git",
4646
"@architect/sandbox": "^6.0.0-RC.1",
47+
"@aws-lite/apigatewaymanagementapi-types": "^0.0.9",
48+
"@aws-lite/dynamodb-types": "^0.3.4",
49+
"@aws-lite/sns-types": "^0.0.5",
50+
"@aws-lite/sqs-types": "^0.2.1",
4751
"@types/aws-lambda": "^8.10.133",
4852
"@types/node": "18",
4953
"cross-env": "~7.0.3",

src/events/index.js

+1-22
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,5 @@ let subFactory = require('./subscribe')
44
module.exports = function eventsAndQueuesFactory (arc, type) {
55
let publish = pubFactory(arc, type)
66
let subscribe = subFactory(type)
7-
return {
8-
/**
9-
* `arc.events|queues.publish`
10-
* publish events and queues
11-
*
12-
* @param {Object} params
13-
* @param {String} params.name - the event name (required)
14-
* @param {Object} params.payload - a json event payload (required)
15-
* @param {Function} callback - a node style errback (optional)
16-
* @returns {Promise} - returned if no callback is supplied
17-
*/
18-
publish,
19-
20-
/**
21-
* `arc.events|queues.subscribe`
22-
* listen for events and queues
23-
*
24-
* @param {Function} handler - a single event handler function
25-
* @returns {Lambda} - a Lambda function sig
26-
*/
27-
subscribe
28-
}
7+
return { publish, subscribe }
298
}

src/static/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ let { join } = require('path')
88
* In order to keep this method sync, it does not use reflection to get fingerprint status
99
* - Not checking @static fingerprint true (which we used to read from the .arc file) is possibly dangerous, so ensure asset path is valid
1010
* - ? TODO: add fingerprint state to env vars in Arc 6 to restore config safety?
11-
* @param {string} path - the path to the asset (eg. /index.js)
11+
* @param {string} asset - the path to the asset (eg. /index.js)
1212
* @returns {string} path - the resolved asset path (eg. /_static/index-xxx.js)
1313
*/
1414
module.exports = function _static (asset, options = {}) {

src/ws/index.js

+2-41
Original file line numberDiff line numberDiff line change
@@ -28,37 +28,18 @@ function instantiateAPI () {
2828
})
2929
}
3030

31-
/**
32-
* arc.ws._api
33-
*
34-
* Get the raw WebSocket client
35-
*
36-
* @param {Function} callback - a node style errback (optional)
37-
* @returns {Promise} - returned if no callback is supplied
38-
*/
3931
function _api (callback) {
4032
if (callback) instantiateAPI()
41-
.then(client => callback(null, client))
33+
.then(client => callback(null, client.ApiGatewayManagementApi))
4234
.catch(callback)
4335

4436
else return new Promise((res, rej) => {
4537
instantiateAPI()
46-
.then(client => res(client))
38+
.then(client => res(client.ApiGatewayManagementApi))
4739
.catch(rej)
4840
})
4941
}
5042

51-
/**
52-
* arc.ws.send
53-
*
54-
* Publish WebSocket events
55-
*
56-
* @param {Object} params
57-
* @param {String} params.id - the ws connection id (required)
58-
* @param {Object} params.payload - an event payload (required)
59-
* @param {Function} callback - a node style errback (optional)
60-
* @returns {Promise} - returned if no callback is supplied
61-
*/
6243
function send ({ id, payload }, callback) {
6344
if (callback) instantiateAPI()
6445
.then(client => {
@@ -87,16 +68,6 @@ function send ({ id, payload }, callback) {
8768
})
8869
}
8970

90-
/**
91-
* arc.ws.close
92-
*
93-
* Terminate a WebSocket client connection
94-
*
95-
* @param {Object} params
96-
* @param {String} params.id - the ws connection id (required)
97-
* @param {Function} callback - a node style errback (optional)
98-
* @returns {Promise} - returned if no callback is supplied
99-
*/
10071
function close ({ id }, callback) {
10172
if (callback) instantiateAPI()
10273
.then(client => {
@@ -123,16 +94,6 @@ function close ({ id }, callback) {
12394
})
12495
}
12596

126-
/**
127-
* arc.ws.info
128-
*
129-
* Get info on a WebSocket client connection
130-
*
131-
* @param {Object} params
132-
* @param {String} params.id - the ws connection id (required)
133-
* @param {Function} callback - a node style errback (optional)
134-
* @returns {Promise} - returned if no callback is supplied
135-
*/
13697
function info ({ id }, callback) {
13798
if (callback) instantiateAPI()
13899
.then(client => {

types/events.d.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { SNS, SQS } from "aws-sdk";
21
import { Callback } from "./util";
2+
import type { PublishResponse as SnsPublishResponse } from "@aws-lite/sns-types"
3+
import type { SendMessageResponse as SqsPublishResponse } from "@aws-lite/sqs-types"
34

45
// Turn off automatic exporting
56
export { };
@@ -26,5 +27,5 @@ interface EventsOrQueues<PublishResult> {
2627
): LambdaFunction;
2728
}
2829

29-
export type ArcEvents = EventsOrQueues<SNS.Types.PublishResponse>;
30-
export type ArcQueues = EventsOrQueues<SQS.Types.SendMessageResult>;
30+
export type ArcEvents = EventsOrQueues<SnsPublishResponse>;
31+
export type ArcQueues = EventsOrQueues<SqsPublishResponse>;

types/http.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {
22
APIGatewayProxyEvent,
33
Context,
44
APIGatewayProxyResult,
5-
} from "aws-lambda";
5+
} from "aws-lambda"; // from @types/aws-lambda
66
import { Callback } from "./util";
77

88
// Turn off automatic exporting

types/index.d.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
/// <reference types="node" />
22

3+
import { ArcEvents, ArcQueues } from "./events";
34
import { ArcHTTP, HttpHandler, HttpAsyncHandler } from "./http";
45
import { ArcStatic } from "./static";
5-
import { ArcWebSocket } from "./ws";
6-
import { ArcEvents, ArcQueues } from "./events";
76
import { ArcTables } from "./tables";
7+
import { ArcWebSocket } from "./ws";
88

99
export type { HttpHandler, HttpAsyncHandler };
1010
export type ArcServices = () => Promise<Record<string, any>>;
1111

12-
export const http: ArcHTTP;
13-
export const static: ArcStatic;
14-
export const ws: ArcWebSocket;
15-
export const services: ArcServices;
1612
export const events: ArcEvents;
13+
export const http: ArcHTTP;
1714
export const queues: ArcQueues;
15+
export const services: ArcServices;
16+
export const static: ArcStatic;
1817
export const tables: ArcTables;
18+
export const ws: ArcWebSocket;

types/index.test-d.ts

+14-8
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,26 @@
1-
import { ApiGatewayManagementApi, DynamoDB, SNS, SQS } from "aws-sdk";
2-
import { Context } from "aws-lambda";
1+
import type { AwsLiteClient } from "@aws-lite/client"
2+
import type { GetConnectionResponse } from "@aws-lite/apigatewaymanagementapi-types";
3+
import type { PublishResponse } from "@aws-lite/sns-types"
4+
import type { SendMessageResponse } from "@aws-lite/sqs-types"
5+
import type { Context } from "aws-lambda";
36
import { expectType, expectAssignable, expectNotAssignable } from "tsd";
47
import arc from "../";
58
import type { HttpHandler, HttpAsyncHandler } from "../"
69
import type { HttpMethods, HttpRequest, HttpResponse } from "./http";
710

11+
// SERVICES
12+
const servicesResult = await arc.services();
13+
expectType<Record<string, any>>(servicesResult);
14+
815
// EVENTS
916
const eventsPublishArg = { name: "test", payload: { foo: "bar" } };
1017
const eventsPublishResult = await arc.events.publish(eventsPublishArg);
11-
expectType<SNS.Types.PublishResponse>(eventsPublishResult);
18+
expectType<PublishResponse>(eventsPublishResult);
1219

1320
// QUEUES
1421
const queuesPublishArg = { name: "test", payload: { foo: "bar" } };
1522
const queuesPublishResult = await arc.queues.publish(queuesPublishArg);
16-
expectType<SQS.Types.SendMessageResult>(queuesPublishResult);
23+
expectType<SendMessageResponse>(queuesPublishResult);
1724

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

95102
// TABLES
96103
const dbClient = await arc.tables()
97-
expectType<DynamoDB>(dbClient._db)
98-
expectType<DynamoDB.DocumentClient>(dbClient._doc)
104+
expectType<AwsLiteClient["DynamoDB"]>(dbClient._client)
99105
expectType<string>(dbClient.name('widgets'))
100106
expectType<Record<string, string>>(dbClient.reflect())
101107
const myTable = dbClient.foobar
@@ -122,9 +128,9 @@ await myTable.scanAll({
122128
})
123129

124130
// WS
125-
expectType<ApiGatewayManagementApi>(arc.ws._api);
131+
expectType<AwsLiteClient["ApiGatewayManagementApi"]>(await arc.ws._api());
126132
expectType<void>(await arc.ws.send({ id: "foo", payload: { bar: "baz" } }));
127133
expectType<void>(await arc.ws.close({ id: "foo" }));
128-
expectType<ApiGatewayManagementApi.Types.GetConnectionResponse>(
134+
expectType<GetConnectionResponse>(
129135
await arc.ws.info({ id: "foo" }),
130136
);

types/tables.d.ts

+12-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import type { DynamoDB } from "aws-sdk";
1+
import type { AwsLiteClient } from "@aws-lite/client"
2+
import type { QueryResponse, ScanResponse, UpdateItemResponse } from "@aws-lite/dynamodb-types"
23
import { Callback } from "./util";
34

45
// Turn off automatic exporting
@@ -17,17 +18,16 @@ type ItemsOutput<OutputType, Item> = Omit<OutputType, "Items"> & {
1718
Items: Item[];
1819
};
1920

20-
type QueryParams = Params<DynamoDB.DocumentClient.QueryInput>;
21-
type QueryOutput<Item> = ItemsOutput<DynamoDB.DocumentClient.QueryOutput, Item>;
21+
type QueryParams = Params<Parameters<AwsLiteClient["DynamoDB"]["Query"]>[0]>;
22+
type QueryOutput<Item> = ItemsOutput<QueryResponse, Item>;
2223

23-
type ScanParams = Params<DynamoDB.DocumentClient.ScanInput>;
24-
type ScanOutput<Item> = ItemsOutput<DynamoDB.DocumentClient.ScanOutput, Item>;
24+
type ScanParams = Params<Parameters<AwsLiteClient["DynamoDB"]["Scan"]>[0]>;
25+
type ScanOutput<Item> = ItemsOutput<ScanResponse, Item>;
2526

2627
type UpdateParams<Item> = ParamsWithKey<
27-
DynamoDB.DocumentClient.UpdateItemInput,
28+
Parameters<AwsLiteClient["DynamoDB"]["UpdateItem"]>[0],
2829
Item
2930
>;
30-
type UpdateOutput = DynamoDB.DocumentClient.UpdateItemOutput;
3131

3232
// Depending on the operation, the key attributes may be mandatory, but we don't
3333
// know what the key attributes are, so Partial is the best we can do.
@@ -51,8 +51,8 @@ export interface ArcTable<Item = unknown> {
5151

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

54-
update(params: UpdateParams<Item>): Promise<UpdateOutput>;
55-
update(params: UpdateParams<Item>, callback: Callback<UpdateOutput>): void;
54+
update(params: UpdateParams<Item>): Promise<UpdateItemResponse>;
55+
update(params: UpdateParams<Item>, callback: Callback<UpdateItemResponse>): void;
5656
}
5757

5858
type ArcDBWith<Tables> = {
@@ -64,8 +64,9 @@ export type ArcDB<Tables> = ArcDBWith<Tables> & {
6464
reflect(): {
6565
[tableName in keyof Tables]: string;
6666
};
67-
_db: DynamoDB;
68-
_doc: DynamoDB.DocumentClient;
67+
_client: AwsLiteClient["DynamoDB"];
68+
// _db: DynamoDB;
69+
// _doc: DynamoDB.DocumentClient;
6970
};
7071

7172
// Permissive by default: allows any table, any inputs, any outputs.

types/ws.d.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { ApiGatewayManagementApi } from "aws-sdk";
1+
import type { AwsLiteClient } from "@aws-lite/client"
2+
import type { GetConnectionResponse } from "@aws-lite/apigatewaymanagementapi-types";
23
import { Callback } from "./util";
34

45
// Turn off automatic exporting
@@ -7,17 +8,17 @@ export { };
78
type SendParams = { id: string; payload: any };
89
type CloseParams = { id: string };
910
type InfoParams = { id: string };
10-
type InfoResponse = ApiGatewayManagementApi.Types.GetConnectionResponse;
1111

1212
export interface ArcWebSocket {
13-
_api: ApiGatewayManagementApi;
13+
_api(): Promise<AwsLiteClient["ApiGatewayManagementApi"]>;
14+
_api(callback: Callback<AwsLiteClient["ApiGatewayManagementApi"]>): void;
1415

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

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

21-
info(params: InfoParams): Promise<InfoResponse>;
22-
info(params: InfoParams, callback: Callback<InfoResponse>): void;
22+
info(params: InfoParams): Promise<GetConnectionResponse>;
23+
info(params: InfoParams, callback: Callback<GetConnectionResponse>): void;
2324
}

0 commit comments

Comments
 (0)