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

Build Typescript and migrate StripeResource #1539

Merged
merged 12 commits into from
Sep 1, 2022
Merged
Show file tree
Hide file tree
Changes from 11 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-var-requires': 0,
'@typescript-eslint/no-explicit-any': 0,
'@typescript-eslint/explicit-function-return-type': 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",
"@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 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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
27 changes: 21 additions & 6 deletions lib/StripeResource.js → src/StripeResource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ const {

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 +123,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 +293,7 @@ StripeResource.prototype = {
this,
new StripeConnectionError({
message: this._generateConnectionErrorMessage(requestRetries),
detail: error,
detail,
}),
null
);
Expand Down Expand Up @@ -356,15 +366,19 @@ StripeResource.prototype = {
sleepSeconds = Math.max(initialNetworkRetryDelay, sleepSeconds);

// And never sleep less than the time the API asks us to wait, assuming it's a reasonable ask.
if (Number.isInteger(retryAfter) && retryAfter <= MAX_RETRY_AFTER_WAIT) {
if (
retryAfter &&
anniel-stripe marked this conversation as resolved.
Show resolved Hide resolved
Number.isInteger(retryAfter) &&
retryAfter <= MAX_RETRY_AFTER_WAIT
) {
sleepSeconds = Math.max(sleepSeconds, retryAfter);
}

return sleepSeconds * 1000;
},

// 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 +494,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 +517,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 +614,7 @@ StripeResource.prototype = {
options.settings
);

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

Expand Down
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.
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.
9 changes: 9 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
anniel-stripe marked this conversation as resolved.
Show resolved Hide resolved
"compilerOptions": {
"outDir": "./lib",
"allowJs": true,
"target": "es6",
"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