-
Notifications
You must be signed in to change notification settings - Fork 41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve TypeScript definition #16
Conversation
WHAT? Moved the typings file from `dist/aws4fetch.d.ts` to `types/index.d.ts`. WHY? Follow the recommendation from [dtslint](https://github.com/microsoft/dtslint).
WHAT? 1. Fixed bugs in definitions: - Renamed `AwsSigner` to `AwsV4Signer`. - Removed `retries` and `initRetryMs` fields from: `AwsSignerInit`. - Removed `body`, `headers`, `method`, and `url` fields from: `AwsRequestInit["aws"]`. - Made all fields optional in `AwsRequestInit["aws"]`. - Removed invalid types (`Blob`, `FormData`, `URLSearchParams`, `ReadableStream`) from `AwsSignerInit["body"]`. - Removed optional suffix (`?`) from required fields. - Removed redundant `| null` type in optional fields. 2. Made all `interface` fields `readonly`. 3. Added `authHeader()` and `signature()` to `AwsV4Signer`. 4. Added `jsdoc` to the TypeScript definitions based on the `README.md` documentation. 5. Added `dtslint`-based tests. WHY? 1. Allow the library to be used correctly with TypeScript. 2. Make the interfaces immutable. 3. Allow invocation of missing public methods. 4. Make the library easier to use. 5. Ensures types are correct and changes are backwards compatible.
@@ -0,0 +1,203 @@ | |||
import { AwsClient, AwsRequestInit, AwsV4Signer } from "aws4fetch"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Previous types don't allow AwsV4Signer
to be imported.
// $ExpectError | ||
new AwsClient({}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Previous types fail this test.
url: "value", | ||
accessKeyId: "value", | ||
secretAccessKey: "value", | ||
retries: 1, // $ExpectError |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Previous types fail this test.
url: "value", | ||
accessKeyId: "value", | ||
secretAccessKey: "value", | ||
initRetryMs: 1, // $ExpectError |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Previous types fail this test.
url: "value", | ||
accessKeyId: "value", | ||
secretAccessKey: "value", | ||
body: new Blob(), // $ExpectError |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Previous types fail this test.
url: "value", | ||
accessKeyId: "value", | ||
secretAccessKey: "value", | ||
body: new FormData(), // $ExpectError |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Previous types fail this test.
url: "value", | ||
accessKeyId: "value", | ||
secretAccessKey: "value", | ||
body: new URLSearchParams(), // $ExpectError |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Previous types fail this test.
url: "value", | ||
accessKeyId: "value", | ||
secretAccessKey: "value", | ||
body: new ReadableStream(), // $ExpectError |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Previous types fail this test.
|
||
function authHeader(signer: AwsV4Signer) { | ||
// $ExpectType Promise<string> | ||
signer.authHeader(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Previous types don't allow this method to be called.
// $ExpectType Promise<AwsRequestInfo<undefined>> | ||
new AwsV4Signer({ | ||
url: "value", | ||
accessKeyId: "value", | ||
secretAccessKey: "value", | ||
}).sign(); | ||
|
||
// $ExpectType Promise<AwsRequestInfo<"value">> | ||
new AwsV4Signer({ | ||
url: "value", | ||
accessKeyId: "value", | ||
secretAccessKey: "value", | ||
body: "value", | ||
}).sign(); | ||
|
||
// $ExpectType Promise<AwsRequestInfo<ArrayBuffer>> | ||
new AwsV4Signer({ | ||
url: "value", | ||
accessKeyId: "value", | ||
secretAccessKey: "value", | ||
body: new ArrayBuffer(1), | ||
}).sign(); | ||
|
||
// $ExpectType Promise<AwsRequestInfo<Uint8Array>> | ||
new AwsV4Signer({ | ||
url: "value", | ||
accessKeyId: "value", | ||
secretAccessKey: "value", | ||
body: new Uint8Array(), | ||
}).sign(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Previous types don't provide generics support for getting a properly types body
.
|
||
function signature(signer: AwsV4Signer) { | ||
// $ExpectType Promise<string> | ||
signer.signature(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Previous types don't allow this method to be called.
Hi @marcogrcr – thanks for the PR! I went with the solution I said I was exploring here: #7 (comment) I think this should cover all the issues you had. I'll close this, but happy to reopen if you think there's something missing |
I'm happy to adjust my pull request to include the missing:
Let me know if you see value in that. |
I don't think I'll add dslint until they remove tslint and move to eslint But happy to look at any jsdoc changes you'd like to make 👍 |
WHAT?
AwsSigner
toAwsV4Signer
.retries
andinitRetryMs
fields from:AwsSignerInit
.body
,headers
,method
, andurl
fields from:AwsRequestInit["aws"]
.AwsRequestInit["aws"]
.Blob
,FormData
,URLSearchParams
,ReadableStream
) fromAwsSignerInit["body"]
.?
) from required fields.| null
type in optional fields.interface
fieldsreadonly
.authHeader()
andsignature()
toAwsV4Signer
.jsdoc
to the TypeScript definitions based on theREADME.md
documentation.dtslint
-based tests.WHY?