Comparing version 1.1.1 to 1.2.0
@@ -13,6 +13,8 @@ declare global { | ||
declare type TokenParams = Record<string, string | number>; | ||
declare type SourceParams = Record<string, string | number>; | ||
export declare type CreateHandler = (status: number, response: Record<string, string>) => void; | ||
export declare type CreateTokenFunction = (type: CreateTokenAsTypes, tokenParams: TokenParams, handler: CreateHandler) => void; | ||
export declare type CreateSourceFunction = (type: string, sourceParams: Record<string, unknown>, handler: CreateHandler) => void; | ||
export declare type CreateSourceFunction = (type: string, sourceParams: SourceParams, handler: CreateHandler) => void; | ||
export declare type CreateTokenPromiseFunction = (type: CreateTokenAsTypes, tokenParams: TokenParams) => Promise<string>; | ||
export declare type CreateSourcePromiseFunction = (type: string, sourceParams: SourceParams) => Promise<string>; | ||
export interface useOmiseReturn { | ||
@@ -25,3 +27,4 @@ loading: boolean; | ||
createTokenPromise: CreateTokenPromiseFunction | null; | ||
createSourcePromise: CreateSourcePromiseFunction | null; | ||
} | ||
export {}; |
@@ -23,3 +23,4 @@ "use strict"; | ||
var _d = react_1.useState(null), createTokenPromiseFn = _d[0], setCreateTokenPromiseFn = _d[1]; | ||
var _e = useOmiseScript_1.useOmiseScript(scriptType), loadingScript = _e[0], errorLoadingScript = _e[1]; | ||
var _e = react_1.useState(null), createSourcePromiseFn = _e[0], setCreateSourcePromiseFn = _e[1]; | ||
var _f = useOmiseScript_1.useOmiseScript(scriptType), loadingScript = _f[0], errorLoadingScript = _f[1]; | ||
react_1.useEffect(function () { | ||
@@ -53,5 +54,21 @@ if (window.Omise) { | ||
}; | ||
// Promisify the original createSource function. | ||
var createSourcePromise = function () { | ||
return function (type, attributes) { | ||
return new Promise(function (resolve, reject) { | ||
omiseCreateSource_1(type, attributes, function (status, response) { | ||
if (status !== 200) { | ||
reject(response); | ||
} | ||
else { | ||
resolve(response === null || response === void 0 ? void 0 : response.id); | ||
} | ||
}); | ||
}); | ||
}; | ||
}; | ||
setCreateTokenFn(createToken); | ||
setCreateSourceFn(createSource); | ||
setCreateTokenPromiseFn(createTokenPromise); | ||
setCreateSourcePromiseFn(createSourcePromise); | ||
} | ||
@@ -64,2 +81,3 @@ }, [loadingScript]); | ||
createTokenPromise: createTokenPromiseFn, | ||
createSourcePromise: createSourcePromiseFn, | ||
checkCreateTokenError: exports.checkCreateTokenError, | ||
@@ -66,0 +84,0 @@ createSource: createSourceFn, |
{ | ||
"name": "use-omise", | ||
"version": "1.1.1", | ||
"version": "1.2.0", | ||
"description": "A React hook for Omise payments", | ||
@@ -5,0 +5,0 @@ "author": "Chris Vibert", |
@@ -62,2 +62,3 @@ # use-omise | ||
createTokenPromise, | ||
createSourcePromise, | ||
createToken, | ||
@@ -69,3 +70,3 @@ createSource, | ||
**Note:** It is recommended that you use the `createTokenPromise` function for creating tokens - this allows you to use `async/await` and promise chaining syntax rather than callbacks. It also uses the `checkCreateTokenError` helper function internally to check for all possible errors. | ||
**Note:** It is recommended that you use the `createTokenPromise` and `createSourcePromise` functions for creating tokens and sources - this allows you to use `async/await` and promise chaining syntax rather than callbacks. The promisified token function also uses the `checkCreateTokenError` helper function internally to check for all possible errors. | ||
@@ -76,2 +77,3 @@ | Value | Type | Description | | ||
| `createTokenPromise` | function | A 'promisified' version of the createToken function | | ||
| `createSourcePromise` | function | A 'promisified' version of the createSource function | | ||
| `createToken` | function | The original Omise createToken function in callback format | | ||
@@ -78,0 +80,0 @@ | `createSource` | function | The original Omise createSource function in callback format | |
19020
239
85