Skip to content

Commit ce6e182

Browse files
Improve error for incompatible card data type in createToken (stripe#19)
Switch order of `createToken` overload. Add test for expected typescript error.
1 parent d12e1b7 commit ce6e182

File tree

5 files changed

+72
-10
lines changed

5 files changed

+72
-10
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"scripts": {
1010
"test": "jest",
1111
"test:versions": "./tests/versions/scripts/test.sh",
12-
"test:types": "tsc -p ./tests/types",
12+
"test:types": "tsc -p ./tests/types && jest --roots '<rootDir>/tests/types'",
1313
"lint": "eslint '{src,types}/**/*.{ts,js}' && yarn prettier-list-different",
1414
"typecheck": "tsc",
1515
"build": "yarn clean && yarn rollup -c",
@@ -55,4 +55,4 @@
5555
"ts-jest": "^24.3.0",
5656
"typescript": "^3.7.4"
5757
}
58-
}
58+
}

tests/types/errors.test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import * as path from 'path';
2+
import {readFileSync} from 'fs';
3+
4+
import * as ts from 'typescript';
5+
6+
const parsedCommandLine = ts.getParsedCommandLineOfConfigFile(
7+
path.resolve(__dirname, 'tsconfig.json'),
8+
{},
9+
{
10+
...ts.sys,
11+
getCurrentDirectory: () => __dirname,
12+
onUnRecoverableConfigFileDiagnostic: () => {},
13+
}
14+
)!;
15+
16+
const getError = function(fileName: string) {
17+
const program = ts.createProgram([fileName], parsedCommandLine.options);
18+
const diagnostics = ts.getPreEmitDiagnostics(program);
19+
20+
return ts.flattenDiagnosticMessageText(diagnostics[0].messageText, '\n');
21+
};
22+
23+
describe('Typescript errors', () => {
24+
test.each([['createCardTokenExtraPropertyError', 'extra_property']])(
25+
'%s',
26+
(fileName, expectedError) => {
27+
expect(
28+
getError(path.resolve(__dirname, `./fixtures/${fileName}.ts`))
29+
).toMatch(expectedError);
30+
}
31+
);
32+
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
///<reference path='../../../types/index.d.ts' />
2+
3+
import {Stripe, StripeElements, StripeCardElement} from '@stripe/stripe-js';
4+
5+
declare const stripe: Stripe;
6+
7+
const elements: StripeElements = stripe.elements();
8+
9+
const cardElement: StripeCardElement = elements.create('card');
10+
11+
stripe.createToken(cardElement, {
12+
currency: '',
13+
name: '',
14+
extra_property: '',
15+
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
///<reference path='../../../types/index.d.ts' />
2+
3+
import {Stripe, StripeElements, StripeIbanElement} from '@stripe/stripe-js';
4+
5+
declare const stripe: Stripe;
6+
7+
const elements: StripeElements = stripe.elements();
8+
9+
const ibanElement: StripeIbanElement = elements.create('iban');
10+
11+
stripe.createToken(ibanElement, {
12+
currency: '',
13+
account_holder_name: '',
14+
extra_property: '',
15+
});

types/stripe-js/index.d.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -252,23 +252,23 @@ declare module '@stripe/stripe-js' {
252252
/////////////////////////////
253253

254254
/**
255-
* Use `stripe.createToken` to convert information collected by card elements into a single-use [Token](https://stripe.com/docs/api#tokens) that you safely pass to your server to use in an API call.
255+
* Use `stripe.createToken` to convert information collected by an `IbanElement` into a single-use [Token](https://stripe.com/docs/api#tokens) that you safely pass to your server to use in an API call.
256256
*
257-
* @docs https://stripe.com/docs/js/tokens_sources/create_token?type=cardElement
257+
* @docs https://stripe.com/docs/js/tokens_sources/create_token?type=ibanElement
258258
*/
259259
createToken(
260-
tokenType: StripeCardElement | StripeCardNumberElement,
261-
data?: CreateTokenCardData
260+
tokenType: StripeIbanElement,
261+
data: CreateTokenIbanData
262262
): Promise<{token?: Token; error?: StripeError}>;
263263

264264
/**
265-
* Use `stripe.createToken` to convert information collected by an `IbanElement` into a single-use [Token](https://stripe.com/docs/api#tokens) that you safely pass to your server to use in an API call.
265+
* Use `stripe.createToken` to convert information collected by card elements into a single-use [Token](https://stripe.com/docs/api#tokens) that you safely pass to your server to use in an API call.
266266
*
267-
* @docs https://stripe.com/docs/js/tokens_sources/create_token?type=ibanElement
267+
* @docs https://stripe.com/docs/js/tokens_sources/create_token?type=cardElement
268268
*/
269269
createToken(
270-
tokenType: StripeIbanElement,
271-
data: CreateTokenIbanData
270+
tokenType: StripeCardElement | StripeCardNumberElement,
271+
data?: CreateTokenCardData
272272
): Promise<{token?: Token; error?: StripeError}>;
273273

274274
/**

0 commit comments

Comments
 (0)