Skip to content

Commit 3978596

Browse files
committed
fix: type definition
1 parent 19e063d commit 3978596

File tree

4 files changed

+2480
-80
lines changed

4 files changed

+2480
-80
lines changed

index.d.ts

Lines changed: 57 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,69 @@
1-
import session, { Store } from "express-session";
1+
import * as express from "express";
2+
import * as session from "express-session";
23
import {
34
DynamoDBClient,
45
GetItemCommandOutput,
56
ProvisionedThroughput,
67
ScalarAttributeType,
78
} from "@aws-sdk/client-dynamodb";
89

10+
export = ConnectDynamoDB;
11+
912
declare function ConnectDynamoDB<Session extends Record<string, unknown>>(
10-
connect: typeof session
11-
): DynamoDBStore<Session>;
12-
export default ConnectDynamoDB;
13+
connect: (options?: session.SessionOptions) => express.RequestHandler
14+
): ConnectDynamoDB.DynamoDBStore<Session>;
1315

14-
export interface DynamoDBStoreOptions {
15-
/** A preconfigured client. If not supplied standard SDK environmental variables will be used. */
16-
client?: DynamoDBClient;
17-
/** Defaults to 'sessions' */
18-
table?: string;
19-
/** Defaults to 'sess:' */
20-
prefix?: string;
21-
/** Defaults to 'id' */
22-
hashKey?: string;
23-
readCapacityUnits?: ProvisionedThroughput["ReadCapacityUnits"];
24-
writeCapacityUnits?: ProvisionedThroughput["WriteCapacityUnits"];
25-
specialKeys?: DynamoDBStoreOptionsSpecialKey[];
26-
skipThrowMissingSpecialKeys?: boolean;
27-
/**
28-
* @deprecated
29-
* Upgrade to DynamoDB's TimeToLive configuration.
30-
*/
31-
reapInterval?: number;
32-
}
16+
declare namespace ConnectDynamoDB {
17+
interface DynamoDBStoreOptions {
18+
/** A preconfigured client. If not supplied standard SDK environmental variables will be used. */
19+
client?: DynamoDBClient;
20+
/** Defaults to 'sessions' */
21+
table?: string;
22+
/** Defaults to 'sess:' */
23+
prefix?: string;
24+
/** Defaults to 'id' */
25+
hashKey?: string;
26+
readCapacityUnits?: ProvisionedThroughput["ReadCapacityUnits"];
27+
writeCapacityUnits?: ProvisionedThroughput["WriteCapacityUnits"];
28+
specialKeys?: DynamoDBStoreOptionsSpecialKey[];
29+
skipThrowMissingSpecialKeys?: boolean;
30+
/**
31+
* @deprecated
32+
* Upgrade to DynamoDB's TimeToLive configuration.
33+
*/
34+
reapInterval?: number;
35+
}
3336

34-
interface DynamoDBStoreOptionsSpecialKey {
35-
name: string; // The session key
36-
type: ScalarAttributeType | "B" | "N" | "S";
37-
}
37+
interface DynamoDBStoreOptionsSpecialKey {
38+
name: string; // The session key
39+
type: ScalarAttributeType | "B" | "N" | "S";
40+
}
3841

39-
export type DynamoDBStore<
40-
Session extends Record<string, unknown> = Record<string, unknown>
41-
> = Store & {
42-
readonly client: DynamoDBClient;
43-
new (options?: DynamoDBStoreOptions): DynamoDBStore<Session>;
44-
initialize(): Promise<void>;
45-
describeSessionsTable(): Promise<void>;
46-
createSessionsTable(): Promise<void>;
47-
get(
48-
id: string,
49-
callback: (err: CallbackError, session?: Session | null) => void
50-
): void;
51-
getParsedSession(
52-
output: Pick<GetItemCommandOutput, "Item">
53-
): Record<string, unknown>;
54-
set(id: string, callback: (err: CallbackError) => void): void;
55-
reap(callback?: (err: CallbackError) => void): void;
56-
destroy(id: string, callback?: (err: CallbackError) => void): void;
57-
getExpiresValue(): number;
58-
touch(
59-
id: string,
60-
callback?: (err: CallbackError, results: { expires: number }) => void
61-
): void;
62-
clearInterval(): void;
63-
};
42+
type DynamoDBStore<
43+
Session extends Record<string, unknown> = Record<string, unknown>
44+
> = session.Store & {
45+
readonly client: DynamoDBClient;
46+
new (options?: DynamoDBStoreOptions): DynamoDBStore<Session>;
47+
initialize(): Promise<void>;
48+
describeSessionsTable(): Promise<void>;
49+
createSessionsTable(): Promise<void>;
50+
get(
51+
id: string,
52+
callback: (err: CallbackError, session?: Session | null) => void
53+
): void;
54+
getParsedSession(
55+
output: Pick<GetItemCommandOutput, "Item">
56+
): Record<string, unknown>;
57+
set(id: string, callback: (err: CallbackError) => void): void;
58+
reap(callback?: (err: CallbackError) => void): void;
59+
destroy(id: string, callback?: (err: CallbackError) => void): void;
60+
getExpiresValue(): number;
61+
touch(
62+
id: string,
63+
callback?: (err: CallbackError, results: { expires: number }) => void
64+
): void;
65+
clearInterval(): void;
66+
};
6467

65-
type CallbackError = Error | undefined | null;
68+
type CallbackError = Error | undefined | null;
69+
}

index.test-d.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { expectType } from "tsd";
2+
3+
import connectDynamoDB, {
4+
DynamoDBStore,
5+
DynamoDBStoreOptions,
6+
DynamoDBStoreOptionsSpecialKey,
7+
} from ".";
8+
import expressSession from "express-session";
9+
import express from "express";
10+
import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
11+
12+
expectType<DynamoDBStore>(connectDynamoDB(expressSession));
13+
expectType<connectDynamoDB.DynamoDBStore>(connectDynamoDB(expressSession));
14+
15+
type SessionData = {
16+
name: string;
17+
animal: "cow" | "pig";
18+
};
19+
const DynamoDBStore: DynamoDBStore =
20+
connectDynamoDB<SessionData>(expressSession);
21+
22+
const specialKeysOption: DynamoDBStoreOptionsSpecialKey = {
23+
name: "userId",
24+
type: "S",
25+
};
26+
const specialKeysOptions: connectDynamoDB.DynamoDBStoreOptionsSpecialKey[] = [
27+
specialKeysOption,
28+
];
29+
const options: DynamoDBStoreOptions = {
30+
table: "myapp-sessions",
31+
client: new DynamoDBClient({ endpoint: "http://localhost:8000" }),
32+
readCapacityUnits: 25,
33+
writeCapacityUnits: 25,
34+
specialKeys: specialKeysOptions,
35+
skipThrowMissingSpecialKeys: true,
36+
};
37+
38+
expectType<express.RequestHandler>(
39+
expressSession({ store: new DynamoDBStore(), secret: "keyboard cat" })
40+
);
41+
expectType<express.RequestHandler>(
42+
expressSession({
43+
store: new DynamoDBStore({ table: "myapp-sessions" }),
44+
secret: "keyboard cat",
45+
})
46+
);
47+
expectType<express.RequestHandler>(
48+
expressSession({
49+
store: new DynamoDBStore(options),
50+
secret: "keyboard cat",
51+
})
52+
);

0 commit comments

Comments
 (0)