hono-stripe-elements 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +149 -0
- package/dist/index.js +82 -0
- package/package.json +46 -4
- package/README.md +0 -3
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import type { PaymentIntentResult, SetupIntentResult, Stripe, StripeAddressElementChangeEvent, StripeCardCvcElement, StripeCardExpiryElement, StripeCardNumberElement, StripeCheckoutConfirmResult, StripeCheckoutElementsSdk, StripeElements, StripeExpressCheckoutElementClickEvent, StripeExpressCheckoutElementConfirmEvent, StripeLinkAuthenticationElement as StripeLinkAuthElement } from '@stripe/stripe-js';
|
|
2
|
+
export type IntentType = 'setup' | 'payment';
|
|
3
|
+
export type StripeLoadedEvent = {
|
|
4
|
+
stripe: Stripe;
|
|
5
|
+
};
|
|
6
|
+
export type FormSubmitEvent = {
|
|
7
|
+
stripe: Stripe;
|
|
8
|
+
cardNumberElement: StripeCardNumberElement;
|
|
9
|
+
cardExpiryElement: StripeCardExpiryElement;
|
|
10
|
+
cardCVCElement: StripeCardCvcElement;
|
|
11
|
+
intentClientSecret?: string;
|
|
12
|
+
zipCode?: string;
|
|
13
|
+
};
|
|
14
|
+
export type DefaultFormSubmitResult = Error | PaymentIntentResult | SetupIntentResult;
|
|
15
|
+
export type PaymentElementSubmitEvent = {
|
|
16
|
+
stripe: Stripe;
|
|
17
|
+
elements?: StripeElements;
|
|
18
|
+
intentClientSecret?: string;
|
|
19
|
+
checkout?: StripeCheckoutElementsSdk;
|
|
20
|
+
checkoutSessionClientSecret?: string;
|
|
21
|
+
};
|
|
22
|
+
export type CheckoutSessionFormSubmitResult = StripeCheckoutConfirmResult | Error;
|
|
23
|
+
export type AddressSubmitEvent = {
|
|
24
|
+
address: {
|
|
25
|
+
value: StripeAddressElementChangeEvent['value'];
|
|
26
|
+
complete: boolean;
|
|
27
|
+
};
|
|
28
|
+
};
|
|
29
|
+
export type LinkAuthenticationElementChangeEvent = {
|
|
30
|
+
stripe: Stripe;
|
|
31
|
+
linkAuthenticationElement: StripeLinkAuthElement;
|
|
32
|
+
email?: string;
|
|
33
|
+
};
|
|
34
|
+
export type CurrencySelectorChangeEvent = {
|
|
35
|
+
currency: string;
|
|
36
|
+
};
|
|
37
|
+
type ElementRef = {
|
|
38
|
+
current: unknown;
|
|
39
|
+
} | ((el: HTMLElement | null) => void | (() => void));
|
|
40
|
+
/**
|
|
41
|
+
* Common props every adapter component accepts on top of its element props.
|
|
42
|
+
*/
|
|
43
|
+
export type StripeElementBaseProps = {
|
|
44
|
+
/**
|
|
45
|
+
* Node rendered until the Web Component module is registered on the client.
|
|
46
|
+
* During SSR and while the module loads, this is rendered instead of the element.
|
|
47
|
+
*/
|
|
48
|
+
fallback?: unknown;
|
|
49
|
+
/** Forwarded to the underlying element instance. */
|
|
50
|
+
ref?: ElementRef;
|
|
51
|
+
};
|
|
52
|
+
/**
|
|
53
|
+
* Creates a hono/jsx component that wraps a stripe-pwa-elements Web Component.
|
|
54
|
+
*
|
|
55
|
+
* - Lazily registers the custom element on the client (`import()` inside an
|
|
56
|
+
* effect), so it is safe in SSR contexts such as HonoX islands.
|
|
57
|
+
* - camelCase props become kebab-case attributes (`buttonLabel` -> `button-label`).
|
|
58
|
+
* - `onXxx` props subscribe the camelCase custom events the components emit
|
|
59
|
+
* (`onFormSubmit` -> `formSubmit`). They are bound synchronously when the
|
|
60
|
+
* element is created, so early events like `stripeLoaded` are not missed.
|
|
61
|
+
* - function/array/object props are assigned as element properties
|
|
62
|
+
* (e.g. `handleSubmit`, `stripeDidLoaded`, `allowedCountries`).
|
|
63
|
+
*/
|
|
64
|
+
export declare function createStripeElementComponent<P extends StripeElementBaseProps>(tag: string, loader: () => Promise<unknown>): (props: P) => import("hono/jsx").JSXNode;
|
|
65
|
+
type StripeCommonProps = {
|
|
66
|
+
publishableKey?: string;
|
|
67
|
+
stripeAccount?: string;
|
|
68
|
+
};
|
|
69
|
+
export type StripeCardElementProps = StripeElementBaseProps & StripeCommonProps & {
|
|
70
|
+
intentClientSecret?: string;
|
|
71
|
+
intentType?: IntentType;
|
|
72
|
+
buttonLabel?: string;
|
|
73
|
+
sheetTitle?: string;
|
|
74
|
+
zip?: boolean;
|
|
75
|
+
showLabel?: boolean;
|
|
76
|
+
applicationName?: string;
|
|
77
|
+
showPaymentRequestButton?: boolean;
|
|
78
|
+
shouldUseDefaultFormSubmitAction?: boolean;
|
|
79
|
+
handleSubmit?: (event: Event, props: FormSubmitEvent) => Promise<void>;
|
|
80
|
+
stripeDidLoaded?: (event: StripeLoadedEvent) => Promise<void>;
|
|
81
|
+
onStripeLoaded?: (event: CustomEvent<StripeLoadedEvent>) => void;
|
|
82
|
+
onFormSubmit?: (event: CustomEvent<FormSubmitEvent>) => void;
|
|
83
|
+
onDefaultFormSubmitResult?: (event: CustomEvent<DefaultFormSubmitResult>) => void;
|
|
84
|
+
};
|
|
85
|
+
export type StripePaymentElementProps = StripeElementBaseProps & StripeCommonProps & {
|
|
86
|
+
intentClientSecret?: string;
|
|
87
|
+
checkoutSessionClientSecret?: string;
|
|
88
|
+
intentType?: IntentType;
|
|
89
|
+
buttonLabel?: string;
|
|
90
|
+
sheetTitle?: string;
|
|
91
|
+
applicationName?: string;
|
|
92
|
+
shouldUseDefaultFormSubmitAction?: boolean;
|
|
93
|
+
handleSubmit?: (event: Event, props: PaymentElementSubmitEvent) => Promise<void>;
|
|
94
|
+
stripeDidLoaded?: (event: StripeLoadedEvent) => Promise<void>;
|
|
95
|
+
onStripeLoaded?: (event: CustomEvent<StripeLoadedEvent>) => void;
|
|
96
|
+
onFormSubmit?: (event: CustomEvent<PaymentElementSubmitEvent>) => void;
|
|
97
|
+
onDefaultFormSubmitResult?: (event: CustomEvent<DefaultFormSubmitResult>) => void;
|
|
98
|
+
onCheckoutSessionConfirmResult?: (event: CustomEvent<CheckoutSessionFormSubmitResult>) => void;
|
|
99
|
+
};
|
|
100
|
+
export type StripeExpressCheckoutElementProps = StripeElementBaseProps & StripeCommonProps & {
|
|
101
|
+
clientSecret?: string;
|
|
102
|
+
intentType?: IntentType;
|
|
103
|
+
amount?: number;
|
|
104
|
+
currency?: string;
|
|
105
|
+
buttonHeight?: string;
|
|
106
|
+
onStripeLoaded?: (event: CustomEvent<StripeLoadedEvent>) => void;
|
|
107
|
+
onConfirm?: (event: CustomEvent<StripeExpressCheckoutElementConfirmEvent>) => void;
|
|
108
|
+
onExpressCheckoutClick?: (event: CustomEvent<StripeExpressCheckoutElementClickEvent>) => void;
|
|
109
|
+
onCancel?: (event: CustomEvent<void>) => void;
|
|
110
|
+
onDefaultConfirmResult?: (event: CustomEvent<DefaultFormSubmitResult>) => void;
|
|
111
|
+
};
|
|
112
|
+
export type StripeAddressElementProps = StripeElementBaseProps & StripeCommonProps & {
|
|
113
|
+
mode?: 'shipping' | 'billing';
|
|
114
|
+
defaultCountry?: string;
|
|
115
|
+
allowedCountries?: string[];
|
|
116
|
+
buttonLabel?: string;
|
|
117
|
+
sheetTitle?: string;
|
|
118
|
+
showLabel?: boolean;
|
|
119
|
+
applicationName?: string;
|
|
120
|
+
handleSubmit?: (event: AddressSubmitEvent) => Promise<void> | void;
|
|
121
|
+
stripeDidLoaded?: (event: StripeLoadedEvent) => Promise<void>;
|
|
122
|
+
onStripeLoaded?: (event: CustomEvent<StripeLoadedEvent>) => void;
|
|
123
|
+
onFormSubmit?: (event: CustomEvent<AddressSubmitEvent>) => void;
|
|
124
|
+
};
|
|
125
|
+
export type StripeLinkAuthenticationElementProps = StripeElementBaseProps & StripeCommonProps & {
|
|
126
|
+
defaultEmail?: string;
|
|
127
|
+
showLabel?: boolean;
|
|
128
|
+
applicationName?: string;
|
|
129
|
+
linkAuthenticationChangeHandler?: (event: LinkAuthenticationElementChangeEvent) => Promise<void> | void;
|
|
130
|
+
stripeDidLoaded?: (event: StripeLoadedEvent) => Promise<void>;
|
|
131
|
+
onStripeLoaded?: (event: CustomEvent<StripeLoadedEvent>) => void;
|
|
132
|
+
onLinkAuthenticationChange?: (event: CustomEvent<LinkAuthenticationElementChangeEvent>) => void;
|
|
133
|
+
};
|
|
134
|
+
export type StripeCurrencySelectorProps = StripeElementBaseProps & StripeCommonProps & {
|
|
135
|
+
clientSecret?: string;
|
|
136
|
+
showLabel?: boolean;
|
|
137
|
+
applicationName?: string;
|
|
138
|
+
currencyChangeHandler?: (props: CurrencySelectorChangeEvent) => Promise<void>;
|
|
139
|
+
stripeDidLoaded?: (event: StripeLoadedEvent) => Promise<void>;
|
|
140
|
+
onStripeLoaded?: (event: CustomEvent<StripeLoadedEvent>) => void;
|
|
141
|
+
onCurrencyChange?: (event: CustomEvent<CurrencySelectorChangeEvent>) => void;
|
|
142
|
+
};
|
|
143
|
+
export declare const StripeCardElement: (props: StripeCardElementProps) => import("hono/jsx").JSXNode;
|
|
144
|
+
export declare const StripePaymentElement: (props: StripePaymentElementProps) => import("hono/jsx").JSXNode;
|
|
145
|
+
export declare const StripeExpressCheckoutElement: (props: StripeExpressCheckoutElementProps) => import("hono/jsx").JSXNode;
|
|
146
|
+
export declare const StripeAddressElement: (props: StripeAddressElementProps) => import("hono/jsx").JSXNode;
|
|
147
|
+
export declare const StripeLinkAuthenticationElement: (props: StripeLinkAuthenticationElementProps) => import("hono/jsx").JSXNode;
|
|
148
|
+
export declare const StripeCurrencySelector: (props: StripeCurrencySelectorProps) => import("hono/jsx").JSXNode;
|
|
149
|
+
export {};
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { useEffect, useState } from 'hono/jsx';
|
|
2
|
+
import { jsx } from 'hono/jsx/jsx-runtime';
|
|
3
|
+
const toKebab = (key) => key.replace(/([A-Z])/g, '-$1').toLowerCase();
|
|
4
|
+
const isEventProp = (key, value) => key.length > 2 && key.startsWith('on') && key[2] === key[2].toUpperCase() && typeof value === 'function';
|
|
5
|
+
const toEventName = (key) => key[2].toLowerCase() + key.slice(3);
|
|
6
|
+
/**
|
|
7
|
+
* Creates a hono/jsx component that wraps a stripe-pwa-elements Web Component.
|
|
8
|
+
*
|
|
9
|
+
* - Lazily registers the custom element on the client (`import()` inside an
|
|
10
|
+
* effect), so it is safe in SSR contexts such as HonoX islands.
|
|
11
|
+
* - camelCase props become kebab-case attributes (`buttonLabel` -> `button-label`).
|
|
12
|
+
* - `onXxx` props subscribe the camelCase custom events the components emit
|
|
13
|
+
* (`onFormSubmit` -> `formSubmit`). They are bound synchronously when the
|
|
14
|
+
* element is created, so early events like `stripeLoaded` are not missed.
|
|
15
|
+
* - function/array/object props are assigned as element properties
|
|
16
|
+
* (e.g. `handleSubmit`, `stripeDidLoaded`, `allowedCountries`).
|
|
17
|
+
*/
|
|
18
|
+
export function createStripeElementComponent(tag, loader) {
|
|
19
|
+
return function StripeElement(props) {
|
|
20
|
+
const [ready, setReady] = useState(false);
|
|
21
|
+
const { fallback, ref: userRef, key: _key, children: _children, ...rest } = props;
|
|
22
|
+
useEffect(() => {
|
|
23
|
+
let alive = true;
|
|
24
|
+
void loader().then(() => {
|
|
25
|
+
if (alive)
|
|
26
|
+
setReady(true);
|
|
27
|
+
});
|
|
28
|
+
return () => {
|
|
29
|
+
alive = false;
|
|
30
|
+
};
|
|
31
|
+
}, []);
|
|
32
|
+
const attributes = {};
|
|
33
|
+
for (const [key, value] of Object.entries(rest)) {
|
|
34
|
+
if (value == null || typeof value === 'function' || typeof value === 'object')
|
|
35
|
+
continue;
|
|
36
|
+
attributes[toKebab(key)] = value === true ? '' : String(value);
|
|
37
|
+
}
|
|
38
|
+
if (!ready)
|
|
39
|
+
return fallback ?? null;
|
|
40
|
+
return jsx(tag, {
|
|
41
|
+
// Bind listeners/properties synchronously when the element is created —
|
|
42
|
+
// events like `stripeLoaded` can fire before effects would run.
|
|
43
|
+
ref: (el) => {
|
|
44
|
+
if (el == null)
|
|
45
|
+
return;
|
|
46
|
+
let userCleanup;
|
|
47
|
+
if (typeof userRef === 'function')
|
|
48
|
+
userCleanup = userRef(el);
|
|
49
|
+
else if (userRef != null)
|
|
50
|
+
userRef.current = el;
|
|
51
|
+
const cleanups = [];
|
|
52
|
+
for (const [key, value] of Object.entries(rest)) {
|
|
53
|
+
if (isEventProp(key, value)) {
|
|
54
|
+
const name = toEventName(key);
|
|
55
|
+
el.addEventListener(name, value);
|
|
56
|
+
cleanups.push(() => el.removeEventListener(name, value));
|
|
57
|
+
}
|
|
58
|
+
else if (value != null && (typeof value === 'function' || typeof value === 'object')) {
|
|
59
|
+
el[key] = value;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return () => {
|
|
63
|
+
for (const cleanup of cleanups)
|
|
64
|
+
cleanup();
|
|
65
|
+
if (userCleanup != null)
|
|
66
|
+
userCleanup();
|
|
67
|
+
else if (typeof userRef === 'function')
|
|
68
|
+
userRef(null);
|
|
69
|
+
else if (userRef != null)
|
|
70
|
+
userRef.current = null;
|
|
71
|
+
};
|
|
72
|
+
},
|
|
73
|
+
...attributes,
|
|
74
|
+
});
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
export const StripeCardElement = createStripeElementComponent('stripe-card-element', () => import('stripe-pwa-elements/dist/components/stripe-card-element'));
|
|
78
|
+
export const StripePaymentElement = createStripeElementComponent('stripe-payment-element', () => import('stripe-pwa-elements/dist/components/stripe-payment-element'));
|
|
79
|
+
export const StripeExpressCheckoutElement = createStripeElementComponent('stripe-express-checkout-element', () => import('stripe-pwa-elements/dist/components/stripe-express-checkout-element'));
|
|
80
|
+
export const StripeAddressElement = createStripeElementComponent('stripe-address-element', () => import('stripe-pwa-elements/dist/components/stripe-address-element'));
|
|
81
|
+
export const StripeLinkAuthenticationElement = createStripeElementComponent('stripe-link-authentication-element', () => import('stripe-pwa-elements/dist/components/stripe-link-authentication-element'));
|
|
82
|
+
export const StripeCurrencySelector = createStripeElementComponent('stripe-currency-selector', () => import('stripe-pwa-elements/dist/components/stripe-currency-selector'));
|
package/package.json
CHANGED
|
@@ -1,6 +1,48 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hono-stripe-elements",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "hono/jsx components wrapping stripe-pwa-elements Web Components",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"files": [
|
|
9
|
+
"dist/"
|
|
10
|
+
],
|
|
11
|
+
"scripts": {
|
|
12
|
+
"build": "tsc",
|
|
13
|
+
"typecheck": "tsc --noEmit",
|
|
14
|
+
"prepublishOnly": "pnpm run build"
|
|
15
|
+
},
|
|
16
|
+
"peerDependencies": {
|
|
17
|
+
"hono": ">=4.0.0",
|
|
18
|
+
"stripe-pwa-elements": ">=4.0.1"
|
|
19
|
+
},
|
|
20
|
+
"devDependencies": {
|
|
21
|
+
"@stripe/stripe-js": "^9.17.0",
|
|
22
|
+
"hono": "^4.13.13",
|
|
23
|
+
"stripe-pwa-elements": "workspace:*",
|
|
24
|
+
"typescript": "^5.9.3"
|
|
25
|
+
},
|
|
26
|
+
"keywords": [
|
|
27
|
+
"hono",
|
|
28
|
+
"honox",
|
|
29
|
+
"jsx",
|
|
30
|
+
"stripe",
|
|
31
|
+
"stripe-elements",
|
|
32
|
+
"payment",
|
|
33
|
+
"web-components"
|
|
34
|
+
],
|
|
35
|
+
"homepage": "https://github.com/hideokamoto/stripe-pwa-elements#readme",
|
|
36
|
+
"bugs": {
|
|
37
|
+
"url": "https://github.com/hideokamoto/stripe-pwa-elements/issues"
|
|
38
|
+
},
|
|
39
|
+
"repository": {
|
|
40
|
+
"type": "git",
|
|
41
|
+
"url": "https://github.com/hideokamoto/stripe-pwa-elements.git",
|
|
42
|
+
"directory": "packages/hono-stripe-elements"
|
|
43
|
+
},
|
|
44
|
+
"publishConfig": {
|
|
45
|
+
"access": "public"
|
|
46
|
+
},
|
|
47
|
+
"license": "MIT"
|
|
48
|
+
}
|
package/README.md
DELETED