Skip to content

Commit

Permalink
Add type information here and there (#1612)
Browse files Browse the repository at this point in the history
  • Loading branch information
pakrym-stripe authored Nov 11, 2022
1 parent 328415a commit 539e72c
Show file tree
Hide file tree
Showing 25 changed files with 837 additions and 386 deletions.
4 changes: 2 additions & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,11 +265,11 @@ module.exports = {
'@typescript-eslint/no-empty-interface': 0,
'@typescript-eslint/no-unused-vars': 0,
'@typescript-eslint/triple-slash-reference': 0,
'@typescript-eslint/ban-ts-ignore': 0,
'@typescript-eslint/ban-ts-comment': 'off',
'@typescript-eslint/no-empty-function': 0,
'@typescript-eslint/camelcase': 0,
'@typescript-eslint/no-explicit-any': 0,
'@typescript-eslint/explicit-function-return-type': 0,
'@typescript-eslint/explicit-function-return-type': 'error',
'@typescript-eslint/no-var-requires': 0,
'prefer-rest-params': 'off',
},
Expand Down
2 changes: 2 additions & 0 deletions lib/Error.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 11 additions & 1 deletion lib/StripeResource.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions lib/Webhooks.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions lib/autoPagination.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions lib/multipart.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions lib/net/FetchHttpClient.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions lib/net/NodeHttpClient.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 12 additions & 8 deletions lib/stripe.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions lib/utils.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 5 additions & 35 deletions src/Error.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,4 @@
/* eslint-disable camelcase */
type RawErrorType =
| 'card_error'
| 'invalid_request_error'
| 'api_error'
| 'idempotency_error'
| 'rate_limit_error'
| 'authentication_error'
| 'invalid_grant';

type StripeRawError = {
message?: string;
type?: RawErrorType;

headers?: {[header: string]: string};
statusCode?: number;
requestId?: string;
code?: string;
doc_url?: string;
decline_code?: string;
param?: string;
detail?: string;
charge?: string;
payment_method_type?: string;

payment_intent?: any;
payment_method?: any;
setup_intent?: any;
source?: any;
exception?: any;
};

/**
* StripeError is the base error from which all other more specific Stripe errors derive.
* Specifically for errors returned from Stripe's REST API.
Expand All @@ -38,9 +7,9 @@ class StripeError extends Error {
readonly message: string;
readonly type: string;
readonly raw: unknown;
readonly rawType: RawErrorType;
readonly headers: {[header: string]: string};
readonly requestId: string;
readonly rawType?: RawErrorType;
readonly headers?: {[header: string]: string};
readonly requestId?: string;

readonly code?: string;
readonly doc_url?: string;
Expand Down Expand Up @@ -69,6 +38,7 @@ class StripeError extends Error {
this.headers = raw.headers;
this.requestId = raw.requestId;
this.statusCode = raw.statusCode;
// @ts-ignore
this.message = raw.message;

this.charge = raw.charge;
Expand All @@ -83,7 +53,7 @@ class StripeError extends Error {
/**
* Helper factory which takes raw stripe errors and outputs wrapping instances
*/
static generate(rawStripeError) {
static generate(rawStripeError: StripeRawError): StripeError {
switch (rawStripeError.type) {
case 'card_error':
return new StripeCardError(rawStripeError);
Expand Down
17 changes: 12 additions & 5 deletions src/ResourceNamespace.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
// ResourceNamespace allows you to create nested resources, i.e. `stripe.issuing.cards`.
// It also works recursively, so you could do i.e. `stripe.billing.invoicing.pay`.

function ResourceNamespace(stripe, resources) {
function ResourceNamespace(
this: StripeResourceNamespaceObject,
stripe: StripeObject,
resources: Array<StripeResourceObject>
): void {
for (const name in resources) {
const camelCaseName = name[0].toLowerCase() + name.substring(1);

const resource = new resources[name](stripe);
const resource = new (resources[name] as any)(stripe);

this[camelCaseName] = resource;
}
}

module.exports = function(namespace, resources) {
return function(stripe) {
return new ResourceNamespace(stripe, resources);
module.exports = function(
namespace: string,
resources: Array<StripeResourceObject>
): (stripe: StripeObject) => StripeResourceNamespaceObject {
return function(stripe: StripeObject): StripeResourceNamespaceObject {
return new (ResourceNamespace as any)(stripe, resources);
};
};

Expand Down
4 changes: 2 additions & 2 deletions src/StripeMethod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ const makeAutoPaginationMethods = autoPagination.makeAutoPaginationMethods;
* Usefully for applying transforms to data on a per-method basis.
* @param [spec.host] Hostname for the request.
*/
function stripeMethod(spec) {
function stripeMethod(spec: MethodSpec): (...args: any[]) => Promise<any> {
if (spec.path !== undefined && spec.fullPath !== undefined) {
throw new Error(
`Method spec specified both a 'path' (${spec.path}) and a 'fullPath' (${spec.fullPath}).`
);
}
return function(...args) {
return function(this: StripeResourceObject, ...args: any[]): Promise<any> {
const callback = typeof args[args.length - 1] == 'function' && args.pop();

spec.urlParams = utils.extractUrlParams(
Expand Down
Loading

0 comments on commit 539e72c

Please sign in to comment.