Skip to content

Commit 921807e

Browse files
committed
Initial commit
0 parents  commit 921807e

File tree

13 files changed

+3429
-0
lines changed

13 files changed

+3429
-0
lines changed

.eslintignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
**/tests/unit/coverage/**
2+
**/node_modules/**
3+
**/dist/**
4+
**/_*
5+
**/*.min.js
6+
7+
**/*.config.js
8+
**/coverage/**
9+
**/scripts/**

.eslintrc.js

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
module.exports = {
2+
root: true,
3+
env: {
4+
node: true,
5+
},
6+
parserOptions: {
7+
ecmaVersion: 2020,
8+
},
9+
extends: [
10+
'plugin:vue/vue3-recommended',
11+
'eslint:recommended',
12+
'@vue/typescript/recommended',
13+
'@vue/prettier/@typescript-eslint',
14+
15+
// https://github.com/standard/standard/blob/master/docs/RULES-en.md
16+
'standard',
17+
// https://github.com/prettier/eslint-config-prettier
18+
'prettier',
19+
'prettier/standard',
20+
'prettier/vue',
21+
'plugin:prettier-vue/recommended',
22+
// https://github.com/jest-community/eslint-plugin-jest
23+
'plugin:jest/recommended',
24+
// https://github.com/sindresorhus/eslint-plugin-unicorn
25+
'plugin:unicorn/recommended',
26+
],
27+
plugins: ['unicorn'],
28+
29+
rules: {
30+
'no-console': process.env.NODE_ENV === 'production' ? 'warn' : 'off',
31+
'no-debugger': process.env.NODE_ENV === 'production' ? 'warn' : 'off',
32+
'no-unreachable': process.env.NODE_ENV === 'production' ? 'error' : 'warn',
33+
34+
// Needed for Vue 3
35+
'vue/no-template-key': 'off',
36+
'vue/require-v-for-key': 'off',
37+
38+
'vue/component-name-in-template-casing': [
39+
'error',
40+
'PascalCase',
41+
{
42+
ignores: [
43+
'component',
44+
'template',
45+
'transition',
46+
'transition-group',
47+
'keep-alive',
48+
'slot',
49+
'i18n',
50+
],
51+
},
52+
],
53+
54+
'unicorn/catch-error-name': [
55+
'error',
56+
{
57+
name: 'error',
58+
ignore: [/^(_|originalError|err)$/],
59+
},
60+
],
61+
62+
'unicorn/filename-case': [
63+
'error',
64+
{
65+
cases: {
66+
kebabCase: true,
67+
},
68+
},
69+
],
70+
'unicorn/prevent-abbreviations': 'off',
71+
'unicorn/no-for-loop': 'off',
72+
'unicorn/no-nested-ternary': 'off',
73+
'unicorn/consistent-function-scoping': 'off',
74+
'unicorn/no-null': 'off',
75+
76+
'@typescript-eslint/ban-ts-ignore': 'off',
77+
},
78+
overrides: [
79+
{
80+
files: ['**/*.unit.ts'],
81+
env: {
82+
jest: true,
83+
},
84+
globals: {
85+
mount: false,
86+
shallowMount: false,
87+
},
88+
},
89+
],
90+
}

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
dist

.prettierignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
**/tests/unit/coverage/**
2+
**/node_modules/**
3+
**/dist/**
4+
**/_*
5+
**/*.min.js
6+
7+
**/*.config.js
8+
**/coverage/**
9+
**/scripts/**

.prettierrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
semi: false
2+
singleQuote: true
3+
trailingComma: es5

README.md

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
# Vue Use Stripe
2+
3+
This is a thin Vue 3 wrapper (0.7 KB gzipped) for Stripe.js written in TypeScript. It simply provides a function (Vue hook) to create Stripe elements and a component that conveniently emits events.
4+
5+
## Installation
6+
7+
Add Stripe.js to `index.html` as recommended by Stripe:
8+
9+
```html
10+
<script async src="https://js.stripe.com/v3/"></script>
11+
```
12+
13+
Alternatively, [install `@stripe/stripe-js`](https://github.com/stripe/stripe-js) and import it in your project to automatically add the previous script tag as a side effect:
14+
15+
```js
16+
// main.js
17+
import '@stripe/stripe-js'
18+
```
19+
20+
Install this wrapper:
21+
22+
```bash
23+
yarn add vue-use-stripe
24+
```
25+
26+
If you are using TypeScript, make sure you also install the mentioned `@stripe/stripe-js` library as well to get proper types for Stripe. Note that, if you are adding the script tag direclty to `index.html`, then `@stripe/stripe-js` can be installed as a **dev dependency** (it will only be used for types, not bundled in your app).
27+
28+
## Usage
29+
30+
```ts
31+
import { defineComponent, ref } from 'vue'
32+
import { useStripe, StripeElement } from 'vue-use-stripe'
33+
34+
export default defineComponent({
35+
components: { StripeElement },
36+
setup() {
37+
const event = ref({})
38+
39+
const {
40+
stripe,
41+
elements: [cardElement],
42+
} = useStripe({
43+
key: process.env.VUE_APP_STRIPE_PUBLIC_KEY || '',
44+
elements: [{ type: 'card', options: {} }],
45+
})
46+
47+
const registerCard = () => {
48+
if (event.complete) {
49+
// Refer to the official docs to see all the Stripe instance properties.
50+
// E.g. https://stripe.com/docs/js/setup_intents/confirm_card_setup
51+
return stripe.value.confirmCardSetup('<client-secret>', {
52+
payment_method: {
53+
card: cardElement.value,
54+
},
55+
})
56+
}
57+
}
58+
59+
return {
60+
event,
61+
cardElement,
62+
registerCard,
63+
}
64+
},
65+
})
66+
```
67+
68+
```html
69+
<template>
70+
<StripeElement :element="cardElement" @change="event = $event" />
71+
<button @click="registerCard">Add</button>
72+
<div v-if="event && event.error">{{ event.error.message }}</div>
73+
</template>
74+
```
75+
76+
### API
77+
78+
```ts
79+
useStripe(options: StripeOptions): {
80+
// Reactive reference to the Stripe instance (created using `window.Stripe`) with proper typings
81+
stripe: Ref<Stripe | null>;
82+
83+
// Reactive reference to the internal elements instance (Stripe.elements(...)).
84+
// This allows creating Stripe elements manually (optional):
85+
// stripeElements.create('card', { <options> })
86+
stripeElements: Ref<StripeElements | null>;
87+
88+
// Array of elements created out of `StripeOptions.elements` array
89+
elements: Ref<any>[];
90+
}
91+
92+
type StripeOptions = {
93+
// Stripe API key
94+
key: string;
95+
96+
// Array of elements to be created
97+
// See the following link for possible types and options:
98+
// https://stripe.com/docs/js/elements_object/create_element?type=card
99+
// E.g. `[{ type: 'card', options: { hidePostalCode: true } }, { type: 'fpxBank' }, ...]
100+
elements?: { type: string; options: object }[];
101+
102+
// Stripe constructor options: https://stripe.com/docs/js/initializing
103+
constructorOptions?: object;
104+
105+
// Elements constructor options: https://stripe.com/docs/js/elements_object/create
106+
elementsOptions?: object;
107+
}
108+
```
109+
110+
Note: `StripeOptions.elements` array is optional. Alternatively, create elements manually using the returned `stripeElements`.
111+
112+
The `<StripeElement />` component will emit any event created by the internal element: `change`, `ready`, `click`, `focus`, `blur`.

package.json

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
{
2+
"name": "vue-use-stripe",
3+
"version": "0.0.1",
4+
"description": "Thin Vue 3 wrapper for Stripe.js",
5+
"main": "dist/index.umd.js",
6+
"module": "dist/index.esm.js",
7+
"unpkg": "dist/index.min.js",
8+
"types": "dist/index.d.ts",
9+
"scripts": {
10+
"test": "echo \"Error: no test specified\" && exit 1",
11+
"build": "rimraf dist && rollup -c",
12+
"lint": "eslint --ext *.ts,.js --fix ./src/* && prettier --list-different --write \"**/*.{ts,js,json}\"",
13+
"prepublishOnly": "yarn lint && yarn build"
14+
},
15+
"repository": {
16+
"type": "git",
17+
"url": "git+https://github.com/frandiox/vue-use-stripe.git"
18+
},
19+
"keywords": [
20+
"stripe",
21+
"elements",
22+
"vue",
23+
"vue3",
24+
"typescript"
25+
],
26+
"author": "Fran Dios <me@frandiox.com>",
27+
"license": "MIT",
28+
"bugs": {
29+
"url": "https://github.com/frandiox/vue-use-stripe/issues"
30+
},
31+
"homepage": "https://github.com/frandiox/vue-use-stripe#readme",
32+
"files": [
33+
"dist/*",
34+
"src/*"
35+
],
36+
"peerDependencies": {
37+
"vue": "^3.0.0-rc.1"
38+
},
39+
"devDependencies": {
40+
"@stripe/stripe-js": "^1.9.0",
41+
"@types/jest": "^26.0.10",
42+
"@typescript-eslint/eslint-plugin": "^2.33.0",
43+
"@typescript-eslint/parser": "^2.33.0",
44+
"@vue/eslint-config-prettier": "6.x",
45+
"@vue/eslint-config-standard": "5.x",
46+
"@vue/eslint-config-typescript": "^5.0.2",
47+
"esbuild": "^0.6.25",
48+
"eslint": "^6.0.0",
49+
"eslint-plugin-import": "^2.22.0",
50+
"eslint-plugin-jest": "^23.20.0",
51+
"eslint-plugin-node": "^11.1.0",
52+
"eslint-plugin-prettier": "^3.1.4",
53+
"eslint-plugin-prettier-vue": "^3.0.0-alpha.2",
54+
"eslint-plugin-promise": "^4.2.1",
55+
"eslint-plugin-standard": "^4.0.1",
56+
"eslint-plugin-typescript": "^0.14.0",
57+
"eslint-plugin-unicorn": "^21.0.0",
58+
"eslint-plugin-vue": "^7.0.0-beta.2",
59+
"prettier": "^2.0.5",
60+
"rimraf": "^3.0.2",
61+
"rollup": "^2.26.3",
62+
"rollup-plugin-dts": "^1.4.10",
63+
"rollup-plugin-esbuild": "^2.4.2",
64+
"typescript": "^3.9.7",
65+
"vue": "^3.0.0-rc.5"
66+
}
67+
}

rollup.config.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import dts from 'rollup-plugin-dts'
2+
import esbuild from 'rollup-plugin-esbuild'
3+
import tsConfig from './tsconfig.json'
4+
import pkgJson from './package.json'
5+
6+
const dist = 'dist'
7+
const name = 'VueUseStripe'
8+
const globals = { vue: 'Vue' }
9+
10+
const baseConfig = {
11+
input: 'src/index.ts',
12+
external: Object.keys(pkgJson.peerDependencies),
13+
plugins: [
14+
esbuild({
15+
target: tsConfig.compilerOptions.target,
16+
minify: true,
17+
}),
18+
],
19+
}
20+
21+
export default [
22+
{
23+
input: baseConfig.input,
24+
output: {
25+
file: dist + '/index.d.ts',
26+
},
27+
plugins: [dts()],
28+
},
29+
{
30+
...baseConfig,
31+
output: {
32+
file: dist + '/index.esm.js',
33+
format: 'esm',
34+
},
35+
},
36+
{
37+
...baseConfig,
38+
output: {
39+
name,
40+
globals,
41+
file: dist + '/index.umd.js',
42+
format: 'umd',
43+
exports: 'named',
44+
compact: true,
45+
},
46+
},
47+
{
48+
...baseConfig,
49+
output: {
50+
name,
51+
globals,
52+
file: dist + '/index.min.js',
53+
format: 'iife',
54+
exports: 'named',
55+
compact: true,
56+
},
57+
},
58+
]

0 commit comments

Comments
 (0)