Skip to content

Commit cdf6013

Browse files
feat(hyper-js): add types for type exposure (#2)
2 parents e808164 + e1f0e68 commit cdf6013

File tree

9 files changed

+321
-15
lines changed

9 files changed

+321
-15
lines changed

build.js

Lines changed: 49 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,64 @@
11
const esbuild = require("esbuild");
2+
const fs = require("fs");
3+
const path = require("path");
24

35
const baseConfig = {
46
entryPoints: ["./src/Index.bs.js"],
57
minify: true,
68
bundle: true,
79
loader: { ".js": "jsx" },
810
plugins: [],
11+
sourcemap: true,
12+
platform: "browser",
13+
target: ["es2018"],
914
};
1015

1116
const builds = [
1217
{ format: "esm", outfile: "dist/index.mjs" },
1318
{ format: "cjs", outfile: "dist/index.js" },
1419
];
1520

16-
Promise.all(
17-
builds.map(({ format, outfile }) =>
18-
esbuild.build({
19-
...baseConfig,
20-
format,
21-
outfile,
22-
})
23-
)
24-
).catch((error) => {
25-
console.error("Build failed:", error);
26-
process.exit(1);
27-
});
21+
async function main() {
22+
try {
23+
// Ensure dist directory exists
24+
if (!fs.existsSync("dist")) {
25+
fs.mkdirSync("dist", { recursive: true });
26+
}
27+
28+
// Copy type definitions to dist
29+
const typeDefSource = path.join(__dirname, "src/index.d.ts");
30+
const typeDefDest = path.join(__dirname, "dist/index.d.ts");
31+
32+
if (fs.existsSync(typeDefSource)) {
33+
fs.copyFileSync(typeDefSource, typeDefDest);
34+
console.log("✓ Type definitions copied to dist");
35+
} else {
36+
console.warn(
37+
"⚠ Warning: Type definitions file not found at src/index.d.ts"
38+
);
39+
}
40+
41+
// Run builds in parallel
42+
await Promise.all(
43+
builds.map(async ({ format, outfile }) => {
44+
try {
45+
await esbuild.build({
46+
...baseConfig,
47+
format,
48+
outfile,
49+
});
50+
console.log(`✓ Built ${format} bundle: ${outfile}`);
51+
} catch (error) {
52+
throw new Error(`Failed to build ${format} bundle: ${error.message}`);
53+
}
54+
})
55+
);
56+
57+
console.log("✓ Build completed successfully");
58+
} catch (error) {
59+
console.error("Build failed:", error);
60+
process.exit(1);
61+
}
62+
}
63+
64+
main();

dist/index.d.ts

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
declare module "@juspay-tech/hyper-js" {
2+
export interface EventData {
3+
iframeMounted: boolean;
4+
focusTriggered: boolean;
5+
blurTriggered: boolean;
6+
clickTriggered: boolean;
7+
elementType: string;
8+
classChange: boolean;
9+
newClassType: string;
10+
}
11+
12+
export interface Event {
13+
key: string;
14+
data: EventData;
15+
}
16+
17+
export type EventParam =
18+
| { type: "Event"; value: Event }
19+
| { type: "EventData"; value: EventData }
20+
| { type: "Empty" };
21+
export type EventHandler = (param: EventParam) => void;
22+
23+
export interface PaymentElement {
24+
on(eventName: string, handler: (param: EventParam) => void): void;
25+
collapse(): void;
26+
blur(): void;
27+
update(options: object): void;
28+
destroy(): void;
29+
unmount(): void;
30+
mount(domElement: string): void;
31+
focus(): void;
32+
clear(): void;
33+
}
34+
35+
export interface Element {
36+
getElement(componentName: string): PaymentElement | null;
37+
update(options: object): void;
38+
fetchUpdates(): Promise<object>;
39+
create(componentType: string, options: object): PaymentElement;
40+
}
41+
42+
export interface ConfirmParams {
43+
return_url: string;
44+
}
45+
46+
export interface ConfirmPaymentParams {
47+
elements: object;
48+
confirmParams: ConfirmParams | null;
49+
}
50+
51+
export interface HyperInstance {
52+
confirmPayment(params: object): Promise<object>;
53+
elements(options: object): Element;
54+
confirmCardPayment(
55+
clientSecret: string,
56+
data?: object,
57+
options?: object
58+
): Promise<object>;
59+
retrievePaymentIntent(paymentIntentId: string): Promise<object>;
60+
widgets(options: object): Element;
61+
paymentRequest(options: object): object;
62+
}
63+
64+
export interface LoadOptions {
65+
customBackendUrl?: string;
66+
env?: "SANDBOX" | "PROD";
67+
[key: string]: any;
68+
}
69+
70+
export type HyperObject =
71+
| string
72+
| {
73+
publishableKey: string;
74+
profileId: string;
75+
};
76+
77+
export interface AnalyticsData {
78+
sessionID: string;
79+
timeStamp: string;
80+
}
81+
82+
/**
83+
* Initializes the Hyper SDK with the given publishable key and options.
84+
*
85+
* @param hyperObject - Either a string publishable key or an object containing publishableKey and profileId
86+
* @param options - Configuration options including customBackendUrl and env settings
87+
* @returns A Promise that resolves to a HyperInstance object
88+
*
89+
* @example
90+
* // Using string publishable key
91+
* const hyperPromise = loadHyper("YOUR_PUBLISHABLE_KEY", {
92+
* customBackendUrl: "YOUR_BACKEND_URL",
93+
* env: "SANDBOX"
94+
* });
95+
*
96+
* // Using object with publishable key
97+
* const hyperPromise = loadHyper({
98+
* publishableKey: "YOUR_PUBLISHABLE_KEY",
99+
* profileId: "YOUR_PROFILE_ID"
100+
* }, {
101+
* customBackendUrl: "YOUR_BACKEND_URL",
102+
* env: "PROD"
103+
* });
104+
*/
105+
export function loadHyper(
106+
hyperObject: HyperObject,
107+
options?: LoadOptions
108+
): Promise<HyperInstance>;
109+
110+
/**
111+
* @deprecated Use loadHyper instead
112+
*/
113+
export function loadStripe(
114+
hyperObject: string | HyperObject,
115+
options?: LoadOptions
116+
): Promise<HyperInstance>;
117+
118+
export type EventType =
119+
| "Escape"
120+
| "Change"
121+
| "Click"
122+
| "Ready"
123+
| "Focus"
124+
| "Blur"
125+
| "None";
126+
}

dist/index.js

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.mjs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
function o(r){if(!(r!==null&&r.BS_PRIVATE_NESTED_SOME_NONE!==void 0))return r;var n=r.BS_PRIVATE_NESTED_SOME_NONE;if(n!==0)return{BS_PRIVATE_NESTED_SOME_NONE:n-1|0}}function m(r,n){if(r<=0)return[];for(var e=new Array(r),t=0;t<r;++t)e[t]=n;return e}function P(r){if(typeof r=="boolean")return r}function T(r){if(r===null)return null}function U(r){if(typeof r=="string")return r}function M(r){if(typeof r=="number")return r}function R(r){if(typeof r=="object"&&!Array.isArray(r)&&r!==null)return r}function B(r){if(Array.isArray(r))return r}var a={bool:P,$$null:T,string:U,$$float:M,object:R,array:B};function k(r){return Math.floor(r)|0}function F(r,n){var e=Math.random()*(n-r|0);return(Math.floor(e)|0)+r|0}var d={floor:k,random:F};function h(r,n,e){return r.reduce(e,n)}function p(r,n){return r!==void 0?o(r):n}function y(){var r="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";return h(m(32,0),"",function(n,e){var t=d.random(0,r.length),l=r.charAt(t);return n+l})}function g(r){var n=r!==void 0?p(a.object(r),{}):{},e=n.env;if(e===void 0)return"";var t=a.string(e);return t!==void 0?t:""}function q(r,n){return new Promise(function(e,t){var l=y(),E=Date.now(),w=g(n),u;switch(w){case"PROD":u="https://checkout.hyperswitch.io/v0/HyperLoader.js";break;case"SANDBOX":u="https://beta.hyperswitch.io/v1/HyperLoader.js";break;default:u=r.startsWith("pk_prd_")?"https://checkout.hyperswitch.io/v0/HyperLoader.js":"https://beta.hyperswitch.io/v1/HyperLoader.js"}var s=Object.fromEntries([["sessionID",l],["timeStamp",E.toString()]]);if(document.querySelectorAll('script[src="'+u+'"]').length===0){var i=document.createElement("script");i.src=u,i.onload=function(){var f=window.Hyper;if(f!=null)return e(f(r,n,s))},i.onerror=function(f){t(f)},document.body.appendChild(i);return}console.warn("INTEGRATION WARNING: There is already an existing script tag for "+u+". Multiple additions of HyperLoader.js is not permitted, please add it on the top level only once.");var v=window.Hyper;if(v!=null)return e(v(r,n,s))})}function Z(r,n){return console.warn("loadStripe is deprecated. Please use loadHyper instead."),q(r,n)}export{q as loadHyper,Z as loadStripe};
1+
function i(r){if(!(r!==null&&r.BS_PRIVATE_NESTED_SOME_NONE!==void 0))return r;var n=r.BS_PRIVATE_NESTED_SOME_NONE;if(n!==0)return{BS_PRIVATE_NESTED_SOME_NONE:n-1|0}}function d(r,n){if(r<=0)return[];for(var e=new Array(r),t=0;t<r;++t)e[t]=n;return e}function D(r){var n=Object.prototype.toString.call(r);switch(n){case"[object Array]":return{TAG:"Array",_0:r};case"[object Boolean]":return{TAG:"Bool",_0:r};case"[object Null]":return"Null";case"[object Number]":return{TAG:"Number",_0:r};case"[object String]":return{TAG:"String",_0:r};default:return{TAG:"Object",_0:r}}}var h={classify:D};function k(r){if(typeof r=="boolean")return r}function J(r){if(r===null)return null}function F(r){if(typeof r=="string")return r}function W(r){if(typeof r=="number")return r}function H(r){if(typeof r=="object"&&!Array.isArray(r)&&r!==null)return r}function L(r){if(Array.isArray(r))return r}var f={bool:k,$$null:J,string:F,$$float:W,object:H,array:L};function q(r){return Math.floor(r)|0}function x(r,n){var e=Math.random()*(n-r|0);return(Math.floor(e)|0)+r|0}var y={floor:q,random:x};function g(r,n,e){return r.reduce(e,n)}function E(r,n){if(r!==void 0)return n(i(r))}function a(r,n){return r!==void 0?i(r):n}function C(){var r="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";return g(d(32,0),"",function(n,e){var t=y.random(0,r.length),o=r.charAt(t);return n+o})}function A(r){var n=r!==void 0?a(f.object(r),{}):{},e=n.env;if(e===void 0)return"";var t=f.string(e);return t!==void 0?t:""}function z(r,n){var e=h.classify(r),t;if(typeof e!="object")t="";else switch(e.TAG){case"String":t=e._0;break;case"Object":t=a(E(e._0.publishableKey,f.string),"");break;default:t=""}return new Promise(function(o,O){var I=C(),N=Date.now(),S=A(n),u;switch(S){case"PROD":u="https://checkout.hyperswitch.io/v0/HyperLoader.js";break;case"SANDBOX":u="https://beta.hyperswitch.io/v1/HyperLoader.js";break;default:u=t.startsWith("pk_prd_")?"https://checkout.hyperswitch.io/v0/HyperLoader.js":"https://beta.hyperswitch.io/v1/HyperLoader.js"}var v=Object.fromEntries([["sessionID",I],["timeStamp",N.toString()]]);if(document.querySelectorAll('script[src="'+u+'"]').length===0){var l=document.createElement("script");l.src=u,l.onload=function(){var c=window.Hyper;if(c!=null)return o(c(r,n,v))},l.onerror=function(c){O(c)},document.body.appendChild(l);return}console.warn("INTEGRATION WARNING: There is already an existing script tag for "+u+". Multiple additions of HyperLoader.js is not permitted, please add it on the top level only once.");var _=window.Hyper;if(_!=null)return o(_(r,n,v))})}function er(r,n){return console.warn("loadStripe is deprecated. Please use loadHyper instead."),z(r,n)}export{z as loadHyper,er as loadStripe};
2+
//# sourceMappingURL=index.mjs.map

dist/index.mjs.map

Lines changed: 7 additions & 0 deletions
Large diffs are not rendered by default.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"module": "index.mjs",
77
"exports": {
88
".": {
9+
"types": "./dist/index.d.ts",
910
"import": "./dist/index.mjs",
1011
"require": "./dist/index.js"
1112
}

rescript.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
],
1010
"package-specs": [
1111
{
12-
"module": "es6",
12+
"module": "esmodule",
1313
"in-source": true
1414
}
1515
],

src/index.d.ts

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
declare module "@juspay-tech/hyper-js" {
2+
export interface EventData {
3+
iframeMounted: boolean;
4+
focusTriggered: boolean;
5+
blurTriggered: boolean;
6+
clickTriggered: boolean;
7+
elementType: string;
8+
classChange: boolean;
9+
newClassType: string;
10+
}
11+
12+
export interface Event {
13+
key: string;
14+
data: EventData;
15+
}
16+
17+
export type EventParam =
18+
| { type: "Event"; value: Event }
19+
| { type: "EventData"; value: EventData }
20+
| { type: "Empty" };
21+
export type EventHandler = (param: EventParam) => void;
22+
23+
export interface PaymentElement {
24+
on(eventName: string, handler: (param: EventParam) => void): void;
25+
collapse(): void;
26+
blur(): void;
27+
update(options: object): void;
28+
destroy(): void;
29+
unmount(): void;
30+
mount(domElement: string): void;
31+
focus(): void;
32+
clear(): void;
33+
}
34+
35+
export interface Element {
36+
getElement(componentName: string): PaymentElement | null;
37+
update(options: object): void;
38+
fetchUpdates(): Promise<object>;
39+
create(componentType: string, options: object): PaymentElement;
40+
}
41+
42+
export interface ConfirmParams {
43+
return_url: string;
44+
}
45+
46+
export interface ConfirmPaymentParams {
47+
elements: object;
48+
confirmParams: ConfirmParams | null;
49+
}
50+
51+
export interface HyperInstance {
52+
confirmPayment(params: object): Promise<object>;
53+
elements(options: object): Element;
54+
confirmCardPayment(
55+
clientSecret: string,
56+
data?: object,
57+
options?: object
58+
): Promise<object>;
59+
retrievePaymentIntent(paymentIntentId: string): Promise<object>;
60+
widgets(options: object): Element;
61+
paymentRequest(options: object): object;
62+
}
63+
64+
export interface LoadOptions {
65+
customBackendUrl?: string;
66+
env?: "SANDBOX" | "PROD";
67+
[key: string]: any;
68+
}
69+
70+
export type HyperObject =
71+
| string
72+
| {
73+
publishableKey: string;
74+
profileId: string;
75+
};
76+
77+
export interface AnalyticsData {
78+
sessionID: string;
79+
timeStamp: string;
80+
}
81+
82+
/**
83+
* Initializes the Hyper SDK with the given publishable key and options.
84+
*
85+
* @param hyperObject - Either a string publishable key or an object containing publishableKey and profileId
86+
* @param options - Configuration options including customBackendUrl and env settings
87+
* @returns A Promise that resolves to a HyperInstance object
88+
*
89+
* @example
90+
* // Using string publishable key
91+
* const hyperPromise = loadHyper("YOUR_PUBLISHABLE_KEY", {
92+
* customBackendUrl: "YOUR_BACKEND_URL",
93+
* env: "SANDBOX"
94+
* });
95+
*
96+
* // Using object with publishable key
97+
* const hyperPromise = loadHyper({
98+
* publishableKey: "YOUR_PUBLISHABLE_KEY",
99+
* profileId: "YOUR_PROFILE_ID"
100+
* }, {
101+
* customBackendUrl: "YOUR_BACKEND_URL",
102+
* env: "PROD"
103+
* });
104+
*/
105+
export function loadHyper(
106+
hyperObject: HyperObject,
107+
options?: LoadOptions
108+
): Promise<HyperInstance>;
109+
110+
/**
111+
* @deprecated Use loadHyper instead
112+
*/
113+
export function loadStripe(
114+
hyperObject: string | HyperObject,
115+
options?: LoadOptions
116+
): Promise<HyperInstance>;
117+
118+
export type EventType =
119+
| "Escape"
120+
| "Change"
121+
| "Click"
122+
| "Ready"
123+
| "Focus"
124+
| "Blur"
125+
| "None";
126+
}

0 commit comments

Comments
 (0)