Skip to content
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

Closed
wants to merge 2 commits into from
Closed

Improve TypeScript definition #16

wants to merge 2 commits into from

Conversation

marcogrcr
Copy link

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.

marcogrcr added 2 commits May 20, 2020 22:02
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";
Copy link
Author

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.

Comment on lines +5 to +6
// $ExpectError
new AwsClient({});
Copy link
Author

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
Copy link
Author

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
Copy link
Author

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
Copy link
Author

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
Copy link
Author

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
Copy link
Author

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
Copy link
Author

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();
Copy link
Author

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.

Comment on lines +167 to +196
// $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();
Copy link
Author

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();
Copy link
Author

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.

@mhart
Copy link
Owner

mhart commented May 22, 2020

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

@mhart mhart closed this May 22, 2020
@marcogrcr
Copy link
Author

I'm happy to adjust my pull request to include the missing:

  • Declaration testing with dtslint for ensuring compatibility and non-breaking changes.
  • jsdoc.

Let me know if you see value in that.

@mhart
Copy link
Owner

mhart commented May 23, 2020

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 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants