From 254bb68aa69bb462c603054659a78089574605ed Mon Sep 17 00:00:00 2001 From: Emmanuel Ohans Date: Fri, 25 Feb 2022 10:52:13 +0100 Subject: [PATCH 1/3] docs(locale): expose type and relevant check --- src/checks.ts | 14 +++++++++++++- src/constants.ts | 12 ++++++++++++ src/index.ts | 7 ++++++- src/types.ts | 17 ++++------------- 4 files changed, 35 insertions(+), 15 deletions(-) diff --git a/src/checks.ts b/src/checks.ts index e430eff..c5cef15 100644 --- a/src/checks.ts +++ b/src/checks.ts @@ -1,4 +1,10 @@ -import { ValidationError, RevolutCheckoutError } from './types' +import { + ValidationError, + RevolutCheckoutError, + RevolutCheckoutLocale, +} from './types' + +import { LOCALES } from './constants' export function isValidationError(error?: Error): error is ValidationError { return error != null && error.name === 'Validation' @@ -9,3 +15,9 @@ export function isRevolutCheckoutError( ): error is RevolutCheckoutError { return error != null && error.name === 'RevolutCheckout' } + +export function isValidRevolutCheckoutLocale( + locale: any +): locale is RevolutCheckoutLocale { + return locale && LOCALES.includes(locale) +} diff --git a/src/constants.ts b/src/constants.ts index 751e077..4051ad6 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -1,3 +1,15 @@ +export const LOCALES = [ + 'en', + 'nl', + 'fr', + 'de', + 'it', + 'lt', + 'pl', + 'pt', + 'es', +] as const + export const MODE = { PRODUCTION: 'prod', DEVELOPMENT: 'dev', diff --git a/src/index.ts b/src/index.ts index 278665e..4a9dc75 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,9 @@ export { RevolutCheckoutLoader as default } from './loader' -export { isRevolutCheckoutError, isValidationError } from './checks' +export { + isValidRevolutCheckoutLocale, + isRevolutCheckoutError, + isValidationError, +} from './checks' export { ValidationErrorType, ValidationError, @@ -8,6 +12,7 @@ export { FieldStatus, FieldClasses, FieldStyles, + RevolutCheckoutLocale, RevolutCheckoutCardField, RevolutCheckoutInstance, } from './types' diff --git a/src/types.ts b/src/types.ts index 833877b..25d91ba 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,4 +1,4 @@ -import { MODE } from './constants' +import { MODE, LOCALES } from './constants' export type Mode = MODE[keyof MODE] @@ -254,16 +254,7 @@ export type CountryCode = | 'QA' | 'MZ' -export type Locale = - | 'en' - | 'nl' - | 'fr' - | 'de' - | 'it' - | 'lt' - | 'pl' - | 'pt' - | 'es' +export type RevolutCheckoutLocale = typeof LOCALES[number] export type ValidationErrorType = | 'validation.card.number.incomplete' @@ -385,7 +376,7 @@ export interface CustomerDetails { export interface CommonOptions { /** Controls the language of the text in the widget method */ - locale?: Locale + locale?: RevolutCheckoutLocale /** Callback will be called when the payment is completed successfully */ onSuccess?: () => void /** Callback if transaction is failed to complete, the reason should be available in the message parameter */ @@ -508,7 +499,7 @@ export interface RevolutCheckoutInstance { /** Manually destroy popup or card field if needed */ destroy: () => void /** Controls the language of the text in the widget */ - setDefaultLocale: (lang: Locale) => void + setDefaultLocale: (lang: RevolutCheckoutLocale) => void } export interface RevolutCheckout { From 0cecd9ada82e46875c76f32afd025c875838e36b Mon Sep 17 00:00:00 2001 From: Emmanuel Ohans Date: Tue, 1 Mar 2022 14:03:11 +0100 Subject: [PATCH 2/3] =?UTF-8?q?=F0=9F=93=9D=20=20docs(locale):=20add=20cze?= =?UTF-8?q?ch=20locale?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/constants.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/constants.ts b/src/constants.ts index 4051ad6..0671980 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -8,6 +8,7 @@ export const LOCALES = [ 'pl', 'pt', 'es', + 'cs', ] as const export const MODE = { From ead73af9321bac7f1d62ef8a13e0be3f72d1aba4 Mon Sep 17 00:00:00 2001 From: Emmanuel Ohans Date: Wed, 2 Mar 2022 12:03:37 +0100 Subject: [PATCH 3/3] docs(locale): rename locale types and guard --- src/checks.ts | 12 +++--------- src/index.ts | 4 ++-- src/types.ts | 6 +++--- 3 files changed, 8 insertions(+), 14 deletions(-) diff --git a/src/checks.ts b/src/checks.ts index c5cef15..1dd1e43 100644 --- a/src/checks.ts +++ b/src/checks.ts @@ -1,8 +1,4 @@ -import { - ValidationError, - RevolutCheckoutError, - RevolutCheckoutLocale, -} from './types' +import { ValidationError, RevolutCheckoutError, Locale } from './types' import { LOCALES } from './constants' @@ -16,8 +12,6 @@ export function isRevolutCheckoutError( return error != null && error.name === 'RevolutCheckout' } -export function isValidRevolutCheckoutLocale( - locale: any -): locale is RevolutCheckoutLocale { - return locale && LOCALES.includes(locale) +export function isValidLocale(locale: unknown): locale is Locale { + return locale && LOCALES.some((value) => value === locale) } diff --git a/src/index.ts b/src/index.ts index 4a9dc75..4cf23d1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,8 +1,8 @@ export { RevolutCheckoutLoader as default } from './loader' export { - isValidRevolutCheckoutLocale, isRevolutCheckoutError, isValidationError, + isValidLocale, } from './checks' export { ValidationErrorType, @@ -12,7 +12,7 @@ export { FieldStatus, FieldClasses, FieldStyles, - RevolutCheckoutLocale, + Locale, RevolutCheckoutCardField, RevolutCheckoutInstance, } from './types' diff --git a/src/types.ts b/src/types.ts index 25d91ba..45e2537 100644 --- a/src/types.ts +++ b/src/types.ts @@ -254,7 +254,7 @@ export type CountryCode = | 'QA' | 'MZ' -export type RevolutCheckoutLocale = typeof LOCALES[number] +export type Locale = typeof LOCALES[number] export type ValidationErrorType = | 'validation.card.number.incomplete' @@ -376,7 +376,7 @@ export interface CustomerDetails { export interface CommonOptions { /** Controls the language of the text in the widget method */ - locale?: RevolutCheckoutLocale + locale?: Locale /** Callback will be called when the payment is completed successfully */ onSuccess?: () => void /** Callback if transaction is failed to complete, the reason should be available in the message parameter */ @@ -499,7 +499,7 @@ export interface RevolutCheckoutInstance { /** Manually destroy popup or card field if needed */ destroy: () => void /** Controls the language of the text in the widget */ - setDefaultLocale: (lang: RevolutCheckoutLocale) => void + setDefaultLocale: (lang: Locale) => void } export interface RevolutCheckout {