Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Migrate other Stripe infrastructure to TS #1563

Merged
merged 13 commits into from
Sep 28, 2022
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ module.exports = {
'@typescript-eslint/no-explicit-any': 0,
'@typescript-eslint/explicit-function-return-type': 0,
'@typescript-eslint/no-var-requires': 0,
'@typescript-eslint/no-this-alias': 0,
'prefer-rest-params': 'off',
},
},
Expand Down
6 changes: 4 additions & 2 deletions lib/Webhooks.js

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

2 changes: 1 addition & 1 deletion lib/crypto/SubtleCryptoProvider.js

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

9 changes: 6 additions & 3 deletions lib/multipart.js

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

3 changes: 2 additions & 1 deletion lib/net/FetchHttpClient.js

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

3 changes: 2 additions & 1 deletion lib/net/NodeHttpClient.js

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

9 changes: 5 additions & 4 deletions lib/utils.js

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

4 changes: 2 additions & 2 deletions src/Error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type StripeRawError = {
doc_url?: string;
decline_code?: string;
param?: string;
detail?: string;
detail?: any;
charge?: string;
payment_method_type?: string;

Expand All @@ -44,7 +44,7 @@ class StripeError extends Error {
readonly code?: string;
readonly doc_url?: string;
readonly param?: string;
readonly detail?: string;
readonly detail?: any;
readonly statusCode?: number;
readonly charge?: string;
readonly decline_code?: string;
Expand Down
File renamed without changes.
10 changes: 6 additions & 4 deletions src/Webhooks.js → src/Webhooks.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
'use strict';

const utils = require('./utils');
const {StripeError, StripeSignatureVerificationError} = require('./Error');
import utils = require('./utils');
import _Error = require('./Error');
const {StripeError, StripeSignatureVerificationError} = _Error;

const Webhook = {
DEFAULT_TOLERANCE: 300, // 5 minutes
signature: null,

constructEvent(payload, header, secret, tolerance, cryptoProvider) {
this.signature.verifyHeader(
Expand Down Expand Up @@ -242,7 +244,7 @@ function parseHeader(header, scheme) {
const kv = item.split('=');

if (kv[0] === 't') {
accum.timestamp = kv[1];
accum.timestamp = parseInt(kv[1], 10);
}

if (kv[0] === scheme) {
Expand Down Expand Up @@ -274,4 +276,4 @@ function getNodeCryptoProvider() {

Webhook.signature = signature;

module.exports = Webhook;
export = Webhook;
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class CryptoProvider {
* - computeHMACSignature('', 'test_secret') => 'f7f9bd47fb987337b5796fdc1fdb9ba221d0d5396814bfcaf9521f43fd8927fd'
* - computeHMACSignature('\ud83d\ude00', 'test_secret') => '837da296d05c4fe31f61d5d7ead035099d9585a5bcde87de952012a78f0b0c43
*/
computeHMACSignature(payload, secret) {
computeHMACSignature(payload: string, secret: string): string {
throw new Error('computeHMACSignature not implemented.');
}

Expand All @@ -28,9 +28,9 @@ class CryptoProvider {
* - computeHMACSignature('', 'test_secret') => 'f7f9bd47fb987337b5796fdc1fdb9ba221d0d5396814bfcaf9521f43fd8927fd'
* - computeHMACSignature('\ud83d\ude00', 'test_secret') => '837da296d05c4fe31f61d5d7ead035099d9585a5bcde87de952012a78f0b0c43
*/
computeHMACSignatureAsync(payload, secret) {
computeHMACSignatureAsync(payload: string, secret: string): Promise<string> {
throw new Error('computeHMACSignatureAsync not implemented.');
}
}

module.exports = CryptoProvider;
export = CryptoProvider;
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
'use strict';

const crypto = require('crypto');
import crypto = require('crypto');

const CryptoProvider = require('./CryptoProvider');
import CryptoProvider = require('./CryptoProvider');

/**
* `CryptoProvider which uses the Node `crypto` package for its computations.
*/
class NodeCryptoProvider extends CryptoProvider {
/** @override */
computeHMACSignature(payload, secret) {
computeHMACSignature(payload: string, secret: string): string {
return crypto
.createHmac('sha256', secret)
.update(payload, 'utf8')
.digest('hex');
}

/** @override */
async computeHMACSignatureAsync(payload, secret) {
async computeHMACSignatureAsync(
payload: string,
secret: string
): Promise<string> {
const signature = await this.computeHMACSignature(payload, secret);
return signature;
}
}

module.exports = NodeCryptoProvider;
export = NodeCryptoProvider;
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
'use strict';

const CryptoProvider = require('./CryptoProvider');
import CryptoProvider = require('./CryptoProvider');

/**
* `CryptoProvider which uses the SubtleCrypto interface of the Web Crypto API.
*
* This only supports asynchronous operations.
*/
class SubtleCryptoProvider extends CryptoProvider {
subtleCrypto: any;

constructor(subtleCrypto) {
super();

Expand All @@ -18,15 +20,15 @@ class SubtleCryptoProvider extends CryptoProvider {
}

/** @override */
computeHMACSignature(payload, secret) {
computeHMACSignature(payload: string, secret: string): string {
throw new Error(
'SubtleCryptoProvider cannot be used in a synchronous context.'
);
}

/** @override */
async computeHMACSignatureAsync(payload, secret) {
const encoder = new TextEncoder('utf-8');
const encoder = new TextEncoder();

const key = await this.subtleCrypto.importKey(
'raw',
Expand Down Expand Up @@ -66,4 +68,4 @@ for (let i = 0; i < byteHexMapping.length; i++) {
byteHexMapping[i] = i.toString(16).padStart(2, '0');
}

module.exports = SubtleCryptoProvider;
export = SubtleCryptoProvider;
11 changes: 7 additions & 4 deletions src/multipart.js → src/multipart.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
'use strict';

const utils = require('./utils');
const {StripeError} = require('./Error');
import utils = require('./utils');
import _Error = require('./Error');
const {StripeError} = _Error;

class StreamProcessingError extends StripeError {}

Expand Down Expand Up @@ -33,7 +34,7 @@ const multipartDataGenerator = (method, data, headers) => {
for (const k in flattenedData) {
const v = flattenedData[k];
push(`--${segno}`);
if (v.hasOwnProperty('data')) {
if (Object.prototype.hasOwnProperty.call(v, 'data')) {
push(
`Content-Disposition: form-data; name=${q(k)}; filename=${q(
v.name || 'blob'
Expand Down Expand Up @@ -93,4 +94,6 @@ const multipartRequestDataProcessor = (method, data, headers, callback) => {
return callback(null, buffer);
};

module.exports.multipartRequestDataProcessor = multipartRequestDataProcessor;
export = {
multipartRequestDataProcessor: multipartRequestDataProcessor,
};
10 changes: 8 additions & 2 deletions src/net/FetchHttpClient.js → src/net/FetchHttpClient.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const {HttpClient, HttpClientResponse} = require('./HttpClient');
import _HttpClient = require('./HttpClient');
const {HttpClient, HttpClientResponse} = _HttpClient;

/**
* HTTP client which uses a `fetch` function to issue requests.
Expand All @@ -11,6 +12,9 @@ const {HttpClient, HttpClientResponse} = require('./HttpClient');
* node-fetch package (https://github.com/node-fetch/node-fetch).
*/
class FetchHttpClient extends HttpClient {
_fetchFn: any;
_res: any;

constructor(fetchFn) {
super();
this._fetchFn = fetchFn;
Expand Down Expand Up @@ -88,6 +92,8 @@ class FetchHttpClient extends HttpClient {
}

class FetchHttpClientResponse extends HttpClientResponse {
_res: any;

constructor(res) {
super(
res.status,
Expand Down Expand Up @@ -135,4 +141,4 @@ class FetchHttpClientResponse extends HttpClientResponse {
}
}

module.exports = {FetchHttpClient, FetchHttpClientResponse};
export = {FetchHttpClient, FetchHttpClientResponse};
14 changes: 12 additions & 2 deletions src/net/HttpClient.js → src/net/HttpClient.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

type TimeoutError = TypeError & {code?: string};

/**
* Encapsulates the logic for issuing a request to the Stripe API.
*
Expand All @@ -10,6 +12,9 @@
* returning their own response class when making requests.
*/
class HttpClient {
static CONNECTION_CLOSED_ERROR_CODES: string[];
static TIMEOUT_ERROR_CODE: string;

/** The client name used for diagnostics. */
getClientName() {
throw new Error('getClientName not implemented.');
Expand All @@ -30,7 +35,9 @@ class HttpClient {

/** Helper to make a consistent timeout error across implementations. */
static makeTimeoutError() {
const timeoutErr = new TypeError(HttpClient.TIMEOUT_ERROR_CODE);
const timeoutErr: TimeoutError = new TypeError(
HttpClient.TIMEOUT_ERROR_CODE
);
timeoutErr.code = HttpClient.TIMEOUT_ERROR_CODE;
return timeoutErr;
}
Expand All @@ -40,6 +47,9 @@ HttpClient.CONNECTION_CLOSED_ERROR_CODES = ['ECONNRESET', 'EPIPE'];
HttpClient.TIMEOUT_ERROR_CODE = 'ETIMEDOUT';

class HttpClientResponse {
_statusCode: number;
_headers: object;

constructor(statusCode, headers) {
this._statusCode = statusCode;
this._headers = headers;
Expand All @@ -66,4 +76,4 @@ class HttpClientResponse {
}
}

module.exports = {HttpClient, HttpClientResponse};
export = {HttpClient, HttpClientResponse};
Loading