Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 49 additions & 12 deletions build.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,64 @@
const esbuild = require("esbuild");
const fs = require("fs");
const path = require("path");

const baseConfig = {
entryPoints: ["./src/Index.bs.js"],
minify: true,
bundle: true,
loader: { ".js": "jsx" },
plugins: [],
sourcemap: true,
platform: "browser",
target: ["es2018"],
};

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

Promise.all(
builds.map(({ format, outfile }) =>
esbuild.build({
...baseConfig,
format,
outfile,
})
)
).catch((error) => {
console.error("Build failed:", error);
process.exit(1);
});
async function main() {
try {
// Ensure dist directory exists
if (!fs.existsSync("dist")) {
fs.mkdirSync("dist", { recursive: true });
}

// Copy type definitions to dist
const typeDefSource = path.join(__dirname, "src/index.d.ts");
const typeDefDest = path.join(__dirname, "dist/index.d.ts");

if (fs.existsSync(typeDefSource)) {
fs.copyFileSync(typeDefSource, typeDefDest);
console.log("✓ Type definitions copied to dist");
} else {
console.warn(
"⚠ Warning: Type definitions file not found at src/index.d.ts"
);
}

// Run builds in parallel
await Promise.all(
builds.map(async ({ format, outfile }) => {
try {
await esbuild.build({
...baseConfig,
format,
outfile,
});
console.log(`✓ Built ${format} bundle: ${outfile}`);
} catch (error) {
throw new Error(`Failed to build ${format} bundle: ${error.message}`);
}
})
);

console.log("✓ Build completed successfully");
} catch (error) {
console.error("Build failed:", error);
process.exit(1);
}
}

main();
123 changes: 123 additions & 0 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
declare module "@juspay-tech/hyper-js" {
export interface EventData {
iframeMounted: boolean;
focusTriggered: boolean;
blurTriggered: boolean;
clickTriggered: boolean;
elementType: string;
classChange: boolean;
newClassType: string;
}

export interface Event {
key: string;
data: EventData;
}

export type EventParam =
| { type: "Event"; value: Event }
| { type: "EventData"; value: EventData }
| { type: "Empty" };
export type EventHandler = (param: EventParam) => void;

export interface PaymentElement {
on(eventName: string, handler: (param: EventParam) => void): void;
collapse(): void;
blur(): void;
update(options: object): void;
destroy(): void;
unmount(): void;
mount(domElement: string): void;
focus(): void;
clear(): void;
}

export interface Element {
getElement(componentName: string): PaymentElement | null;
update(options: object): void;
fetchUpdates(): Promise<object>;
create(componentType: string, options: object): PaymentElement;
}

export interface ConfirmParams {
return_url: string;
}

export interface ConfirmPaymentParams {
elements: object;
confirmParams: ConfirmParams | null;
}

export interface HyperInstance {
confirmPayment(params: object): Promise<object>;
elements(options: object): Element;
confirmCardPayment(
clientSecret: string,
data?: object,
options?: object
): Promise<object>;
retrievePaymentIntent(paymentIntentId: string): Promise<object>;
widgets(options: object): Element;
paymentRequest(options: object): object;
}

export interface LoadOptions {
customBackendUrl?: string;
env?: "SANDBOX" | "PROD";
[key: string]: any;
}

export interface HyperObject {
publishableKey?: string;
[key: string]: any;
}

export interface AnalyticsData {
sessionID: string;
timeStamp: string;
}

/**
* Initializes the Hyper SDK with the given publishable key and options.
*
* @param hyperObject - Either a string publishable key or an object containing publishableKey
* @param options - Configuration options including customBackendUrl and env settings
* @returns A Promise that resolves to a HyperInstance object
*
* @example
* // Using string publishable key
* const hyperPromise = loadHyper("YOUR_PUBLISHABLE_KEY", {
* customBackendUrl: "YOUR_BACKEND_URL",
* env: "SANDBOX"
* });
*
* // Using object with publishable key
* const hyperPromise = loadHyper({
* publishableKey: "YOUR_PUBLISHABLE_KEY"
* }, {
* customBackendUrl: "YOUR_BACKEND_URL",
* env: "PROD"
* });
*/
export function loadHyper(
hyperObject: string | HyperObject,
options?: LoadOptions
): Promise<HyperInstance>;

/**
* @deprecated Use loadHyper instead
*/
export function loadStripe(
hyperObject: string | HyperObject,
options?: LoadOptions
): Promise<HyperInstance>;

export type EventType =
| "Escape"
| "Change"
| "Click"
| "Ready"
| "Focus"
| "Blur"
| "None";
}
3 changes: 2 additions & 1 deletion dist/index.js

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

7 changes: 7 additions & 0 deletions dist/index.js.map

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion dist/index.mjs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
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};
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};
//# sourceMappingURL=index.mjs.map
7 changes: 7 additions & 0 deletions dist/index.mjs.map

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"module": "index.mjs",
"exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.mjs",
"require": "./dist/index.js"
}
Expand Down
2 changes: 1 addition & 1 deletion rescript.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
],
"package-specs": [
{
"module": "es6",
"module": "esmodule",
"in-source": true
}
],
Expand Down
123 changes: 123 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
declare module "@juspay-tech/hyper-js" {
export interface EventData {
iframeMounted: boolean;
focusTriggered: boolean;
blurTriggered: boolean;
clickTriggered: boolean;
elementType: string;
classChange: boolean;
newClassType: string;
}

export interface Event {
key: string;
data: EventData;
}

export type EventParam =
| { type: "Event"; value: Event }
| { type: "EventData"; value: EventData }
| { type: "Empty" };
export type EventHandler = (param: EventParam) => void;

export interface PaymentElement {
on(eventName: string, handler: (param: EventParam) => void): void;
collapse(): void;
blur(): void;
update(options: object): void;
destroy(): void;
unmount(): void;
mount(domElement: string): void;
focus(): void;
clear(): void;
}

export interface Element {
getElement(componentName: string): PaymentElement | null;
update(options: object): void;
fetchUpdates(): Promise<object>;
create(componentType: string, options: object): PaymentElement;
}

export interface ConfirmParams {
return_url: string;
}

export interface ConfirmPaymentParams {
elements: object;
confirmParams: ConfirmParams | null;
}

export interface HyperInstance {
confirmPayment(params: object): Promise<object>;
elements(options: object): Element;
confirmCardPayment(
clientSecret: string,
data?: object,
options?: object
): Promise<object>;
retrievePaymentIntent(paymentIntentId: string): Promise<object>;
widgets(options: object): Element;
paymentRequest(options: object): object;
}

export interface LoadOptions {
customBackendUrl?: string;
env?: "SANDBOX" | "PROD";
[key: string]: any;
}

export interface HyperObject {
publishableKey?: string;
[key: string]: any;
}

export interface AnalyticsData {
sessionID: string;
timeStamp: string;
}

/**
* Initializes the Hyper SDK with the given publishable key and options.
*
* @param hyperObject - Either a string publishable key or an object containing publishableKey
* @param options - Configuration options including customBackendUrl and env settings
* @returns A Promise that resolves to a HyperInstance object
*
* @example
* // Using string publishable key
* const hyperPromise = loadHyper("YOUR_PUBLISHABLE_KEY", {
* customBackendUrl: "YOUR_BACKEND_URL",
* env: "SANDBOX"
* });
*
* // Using object with publishable key
* const hyperPromise = loadHyper({
* publishableKey: "YOUR_PUBLISHABLE_KEY"
* }, {
* customBackendUrl: "YOUR_BACKEND_URL",
* env: "PROD"
* });
*/
export function loadHyper(
hyperObject: string | HyperObject,
options?: LoadOptions
): Promise<HyperInstance>;

/**
* @deprecated Use loadHyper instead
*/
export function loadStripe(
hyperObject: string | HyperObject,
options?: LoadOptions
): Promise<HyperInstance>;

export type EventType =
| "Escape"
| "Change"
| "Click"
| "Ready"
| "Focus"
| "Blur"
| "None";
}
Loading