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

Re-introduce Typescript changes #1551

Merged
merged 15 commits into from
Sep 22, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
*.node*.js
node_modules
node_modules
lib
26 changes: 26 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,4 +249,30 @@ module.exports = {
},
plugins: ['prettier'],
extends: ['plugin:prettier/recommended'],
overrides: [
{
files: ["**/*.ts"],
parser: "@typescript-eslint/parser",
plugins: ['@typescript-eslint', 'prettier'],
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/eslint-recommended',
'plugin:@typescript-eslint/recommended',
'plugin:prettier/recommended',
],
rules: {
'@typescript-eslint/no-use-before-define': 0,
'@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/no-empty-function': 0,
'@typescript-eslint/camelcase': 0,
'@typescript-eslint/no-explicit-any': 0,
'@typescript-eslint/explicit-function-return-type': 0,
'@typescript-eslint/no-var-requires': 0,
'prefer-rest-params': 'off',
},
},
],
};
6 changes: 3 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ jobs:
restore-keys: |
${{ runner.os }}-yarn-

- name: Node check
run: find . -name "*.js" -type f -not -path "./node_modules/*" -not -path "./\.*" -exec node --check {} \;
- name: Build Typescript
run: yarn && yarn build

- name: Lint
run: yarn && yarn lint
run: yarn lint

test:
name: Test (${{ matrix.node }})
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ tags
.nyc_output
coverage
.idea
lib
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"main": "lib/stripe.js",
"types": "types/2022-08-01/index.d.ts",
"devDependencies": {
"@istanbuljs/nyc-config-typescript": "^1.0.2",
pakrym-stripe marked this conversation as resolved.
Show resolved Hide resolved
"@typescript-eslint/eslint-plugin": "^2.13.0",
"@typescript-eslint/parser": "^2.13.0",
"chai": "~4.2.0",
Expand Down Expand Up @@ -55,10 +56,12 @@
},
"license": "MIT",
"scripts": {
"clean": "rm -rf ./.nyc_output ./node_modules/.cache ./coverage",
"build": "tsc -p tsconfig.json",
"clean": "rm -rf ./.nyc_output ./node_modules/.cache ./coverage ./lib",
"prepack": "yarn install && yarn build",
"mocha": "nyc mocha --config=test/.mocharc.js",
"mocha-only": "mocha --config=test/.mocharc.js",
"test": "yarn test-typescript && yarn mocha",
"test": "yarn build && yarn test-typescript && yarn mocha",
"test-typescript": "tsc --build types/test",
"lint": "eslint --ext .js,.jsx,.ts .",
"fix": "yarn lint --fix && ./scripts/updateAPIVersion.js",
Expand Down
54 changes: 53 additions & 1 deletion lib/Error.js → src/Error.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,63 @@
'use strict';

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.
*/
class StripeError extends Error {
constructor(raw = {}) {
readonly message: string;
readonly type: string;
readonly raw: unknown;
readonly rawType: RawErrorType;
readonly headers: {[header: string]: string};
readonly requestId: string;

readonly code?: string;
readonly doc_url?: string;
readonly param?: string;
readonly detail?: string;
readonly statusCode?: number;
readonly charge?: string;
readonly decline_code?: string;
readonly payment_method_type?: string;

readonly payment_intent?: any;
readonly payment_method?: any;
readonly setup_intent?: any;
readonly source?: any;

constructor(raw: StripeRawError) {
super(raw.message);
this.type = this.constructor.name;

Expand Down
File renamed without changes.
File renamed without changes.
8 changes: 3 additions & 5 deletions lib/StripeMethod.js → src/StripeMethod.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict';

const utils = require('./utils');
const makeRequest = require('./makeRequest');
import * as utils from './utils';
pakrym-stripe marked this conversation as resolved.
Show resolved Hide resolved
import makeRequest = require('./makeRequest');
const makeAutoPaginationMethods = require('./autoPagination')
.makeAutoPaginationMethods;

Expand Down Expand Up @@ -55,4 +53,4 @@ function stripeMethod(spec) {
};
}

module.exports = stripeMethod;
export = stripeMethod;
33 changes: 21 additions & 12 deletions lib/StripeResource.js → src/StripeResource.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
'use strict';

const utils = require('./utils');
import * as utils from './utils';
const {
StripeConnectionError,
StripeAPIError,
StripeAuthenticationError,
StripeConnectionError,
StripeError,
StripePermissionError,
StripeRateLimitError,
StripeError,
StripeAPIError,
} = require('./Error');

const {HttpClient} = require('./net/HttpClient');

type Settings = {
timeout?: number;
};

type Options = {
settings?: Settings;
streaming?: boolean;
headers?: Record<string, string>;
};

// Provide extension mechanism for Stripe Resource Sub-Classes
StripeResource.extend = utils.protoExtend;

Expand Down Expand Up @@ -113,7 +121,7 @@ StripeResource.prototype = {
_timeoutHandler(timeout, req, callback) {
return () => {
const timeoutErr = new TypeError('ETIMEDOUT');
timeoutErr.code = 'ETIMEDOUT';
(timeoutErr as any).code = 'ETIMEDOUT';

req.destroy(timeoutErr);
};
Expand Down Expand Up @@ -283,7 +291,7 @@ StripeResource.prototype = {
this,
new StripeConnectionError({
message: this._generateConnectionErrorMessage(requestRetries),
detail: error,
detail,
}),
null
);
Expand Down Expand Up @@ -364,7 +372,7 @@ StripeResource.prototype = {
},

// Max retries can be set on a per request basis. Favor those over the global setting
_getMaxNetworkRetries(settings = {}) {
_getMaxNetworkRetries(settings: {maxNetworkRetries?: number} = {}) {
return settings.maxNetworkRetries &&
Number.isInteger(settings.maxNetworkRetries)
? settings.maxNetworkRetries
Expand Down Expand Up @@ -480,7 +488,7 @@ StripeResource.prototype = {
}
},

_request(method, host, path, data, auth, options = {}, callback) {
_request(method, host, path, data, auth, options: Options = {}, callback) {
let requestData;

const retryRequest = (
Expand All @@ -503,6 +511,7 @@ StripeResource.prototype = {
// timeout can be set on a per-request basis. Favor that over the global setting
const timeout =
options.settings &&
options.settings.timeout &&
Number.isInteger(options.settings.timeout) &&
options.settings.timeout >= 0
? options.settings.timeout
Expand Down Expand Up @@ -599,7 +608,7 @@ StripeResource.prototype = {
options.settings
);

makeRequest(apiVersion, headers);
makeRequest(apiVersion, headers, 0);
});
};

Expand All @@ -616,4 +625,4 @@ StripeResource.prototype = {
},
};

module.exports = StripeResource;
export = StripeResource;
File renamed without changes.
File renamed without changes.
10 changes: 5 additions & 5 deletions lib/autoPagination.js → src/autoPagination.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const makeRequest = require('./makeRequest');
anniel-stripe marked this conversation as resolved.
Show resolved Hide resolved
const utils = require('./utils');
const utils_1 = require('./utils');
anniel-stripe marked this conversation as resolved.
Show resolved Hide resolved

function makeAutoPaginationMethods(self, requestArgs, spec, firstPagePromise) {
const promiseCache = {currentPromise: null};
Expand Down Expand Up @@ -207,7 +207,7 @@ function makeAutoPagingEach(asyncIteratorNext) {
asyncIteratorNext,
onItem
);
return utils.callbackifyPromiseWithTimeout(autoPagePromise, onDone);
return utils_1.callbackifyPromiseWithTimeout(autoPagePromise, onDone);
};
}

Expand Down Expand Up @@ -237,12 +237,12 @@ function makeAutoPagingToArray(autoPagingEach) {
})
.catch(reject);
});
return utils.callbackifyPromiseWithTimeout(promise, onDone);
return utils_1.callbackifyPromiseWithTimeout(promise, onDone);
};
}

function wrapAsyncIteratorWithCallback(asyncIteratorNext, onItem) {
return new Promise((resolve, reject) => {
return new Promise<void>((resolve, reject) => {
function handleIteration(iterResult) {
if (iterResult.done) {
resolve();
Expand Down Expand Up @@ -272,7 +272,7 @@ function wrapAsyncIteratorWithCallback(asyncIteratorNext, onItem) {

function isReverseIteration(requestArgs) {
const args = [].slice.call(requestArgs);
const dataFromArgs = utils.getDataFromArgs(args);
const dataFromArgs = utils_1.getDataFromArgs(args);

return !!dataFromArgs.ending_before;
}
File renamed without changes.
File renamed without changes.
6 changes: 2 additions & 4 deletions lib/makeRequest.js → src/makeRequest.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
'use strict';

const utils = require('./utils');
import * as utils from './utils';
anniel-stripe marked this conversation as resolved.
Show resolved Hide resolved

function getRequestOpts(self, requestArgs, spec, overrideData) {
// Extract spec values with defaults.
Expand Down Expand Up @@ -119,4 +117,4 @@ function makeRequest(self, requestArgs, spec, overrideData) {
});
}

module.exports = makeRequest;
export = makeRequest;
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
21 changes: 12 additions & 9 deletions lib/stripe.js → src/stripe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ Stripe.CryptoProvider = CryptoProvider;

function Stripe(key, config = {}) {
if (!(this instanceof Stripe)) {
return new Stripe(key, config);
return new (Stripe as any)(key, config);
}

const props = this._getPropsFromConfig(config);
Expand Down Expand Up @@ -326,15 +326,18 @@ Stripe.prototype = {

info = info || {};

const appInfo = APP_INFO_PROPERTIES.reduce((accum, prop) => {
if (typeof info[prop] == 'string') {
accum = accum || {};
const appInfo = APP_INFO_PROPERTIES.reduce(
(accum: Record<string, any>, prop) => {
if (typeof info[prop] == 'string') {
accum = accum || {};

accum[prop] = info[prop];
}
accum[prop] = info[prop];
}

return accum;
}, undefined);
return accum;
},
undefined
);

this._appInfo = appInfo;
},
Expand Down Expand Up @@ -483,7 +486,7 @@ Stripe.prototype = {
*/
getClientUserAgentSeeded(seed, cb) {
this.getUname((uname) => {
const userAgent = {};
const userAgent: Record<string, string> = {};
for (const field in seed) {
userAgent[field] = encodeURIComponent(seed[field]);
}
Expand Down
File renamed without changes.
10 changes: 10 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"compilerOptions": {
"outDir": "./lib",
"allowJs": true,
"target": "es6",
"module": "commonjs",
"checkJs": false,
},
"include": ["./src/**/*"]
}
7 changes: 7 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,13 @@
js-yaml "^3.13.1"
resolve-from "^5.0.0"

"@istanbuljs/nyc-config-typescript@^1.0.2":
version "1.0.2"
resolved "https://registry.yarnpkg.com/@istanbuljs/nyc-config-typescript/-/nyc-config-typescript-1.0.2.tgz#1f5235b28540a07219ae0dd42014912a0b19cf89"
integrity sha512-iKGIyMoyJuFnJRSVTZ78POIRvNnwZaWIf8vG4ZS3rQq58MMDrqEX2nnzx0R28V2X8JvmKYiqY9FP2hlJsm8A0w==
dependencies:
"@istanbuljs/schema" "^0.1.2"

"@istanbuljs/schema@^0.1.2":
version "0.1.3"
resolved "https://registry.yarnpkg.com/@istanbuljs/schema/-/schema-0.1.3.tgz#e45e384e4b8ec16bce2fd903af78450f6bf7ec98"
Expand Down