@fat-zebra/sdk 1.0.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/.jest/setup-env-vars.js +2 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -0
- package/dist/react/VerifyCard.d.ts +10 -0
- package/dist/react/VerifyCard.js +22 -0
- package/dist/react/VerifyCard.js.map +1 -0
- package/dist/react/index.d.ts +2 -0
- package/dist/react/index.js +9 -0
- package/dist/react/index.js.map +1 -0
- package/dist/react/url.d.ts +26 -0
- package/dist/react/url.js +63 -0
- package/dist/react/url.js.map +1 -0
- package/dist/react/useFatZebra.d.ts +12 -0
- package/dist/react/useFatZebra.js +148 -0
- package/dist/react/useFatZebra.js.map +1 -0
- package/dist/sca/cardinal.d.ts +28 -0
- package/dist/sca/cardinal.js +110 -0
- package/dist/sca/cardinal.js.map +1 -0
- package/dist/sca/eci-mappings.d.ts +6 -0
- package/dist/sca/eci-mappings.js +63 -0
- package/dist/sca/eci-mappings.js.map +1 -0
- package/dist/sca/index.d.ts +38 -0
- package/dist/sca/index.js +395 -0
- package/dist/sca/index.js.map +1 -0
- package/dist/sca/scenarios/enrollment.d.ts +15 -0
- package/dist/sca/scenarios/enrollment.js +150 -0
- package/dist/sca/scenarios/enrollment.js.map +1 -0
- package/dist/sca/scenarios/index.d.ts +2 -0
- package/dist/sca/scenarios/index.js +8 -0
- package/dist/sca/scenarios/index.js.map +1 -0
- package/dist/sca/scenarios/validation.d.ts +15 -0
- package/dist/sca/scenarios/validation.js +128 -0
- package/dist/sca/scenarios/validation.js.map +1 -0
- package/dist/sca/types.d.ts +158 -0
- package/dist/sca/types.js +57 -0
- package/dist/sca/types.js.map +1 -0
- package/dist/shared/api-gateway-client.d.ts +17 -0
- package/dist/shared/api-gateway-client.js +106 -0
- package/dist/shared/api-gateway-client.js.map +1 -0
- package/dist/shared/bridge-client.d.ts +2 -0
- package/dist/shared/bridge-client.js +24 -0
- package/dist/shared/bridge-client.js.map +1 -0
- package/dist/shared/constants.d.ts +6 -0
- package/dist/shared/constants.js +19 -0
- package/dist/shared/constants.js.map +1 -0
- package/dist/shared/event-manager.d.ts +28 -0
- package/dist/shared/event-manager.js +15 -0
- package/dist/shared/event-manager.js.map +1 -0
- package/dist/shared/post-message-client.d.ts +47 -0
- package/dist/shared/post-message-client.js +107 -0
- package/dist/shared/post-message-client.js.map +1 -0
- package/dist/shared/types.d.ts +142 -0
- package/dist/shared/types.js +41 -0
- package/dist/shared/types.js.map +1 -0
- package/dist/shared/util.d.ts +6 -0
- package/dist/shared/util.js +96 -0
- package/dist/shared/util.js.map +1 -0
- package/fatzebra-sdk-1.0.0.tgz +0 -0
- package/index.js +0 -0
- package/jest.config.js +16 -0
- package/package.json +33 -0
- package/src/index.ts +9 -0
- package/src/react/VerifyCard.tsx +36 -0
- package/src/react/index.ts +3 -0
- package/src/react/url.ts +97 -0
- package/src/react/useFatZebra.ts +154 -0
- package/src/sca/__tests__/eci-mappings.test.ts +35 -0
- package/src/sca/__tests__/index.test.ts +514 -0
- package/src/sca/cardinal.ts +136 -0
- package/src/sca/eci-mappings.ts +63 -0
- package/src/sca/index.ts +329 -0
- package/src/sca/scenarios/enrollment.ts +164 -0
- package/src/sca/scenarios/index.ts +2 -0
- package/src/sca/scenarios/validation.ts +142 -0
- package/src/sca/types.ts +206 -0
- package/src/shared/api-gateway-client.ts +71 -0
- package/src/shared/bridge-client.ts +26 -0
- package/src/shared/constants.ts +19 -0
- package/src/shared/event-manager.ts +44 -0
- package/src/shared/post-message-client.test.ts +73 -0
- package/src/shared/post-message-client.ts +125 -0
- package/src/shared/types.test.ts +65 -0
- package/src/shared/types.ts +182 -0
- package/src/shared/util.test.ts +164 -0
- package/src/shared/util.ts +98 -0
- package/tests/helpers/api-gateway-mock.ts +46 -0
- package/tsconfig.json +22 -0
- package/yarn-error.log +2374 -0
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { Customer } from './types'
|
|
2
|
+
|
|
3
|
+
describe('Customer', () => {
|
|
4
|
+
|
|
5
|
+
const declareCustomer = (properties = {}) => {
|
|
6
|
+
const customer: Customer = properties
|
|
7
|
+
return customer
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
it('allows no properties', () => {
|
|
11
|
+
expect(() => declareCustomer()).not.toThrow()
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
it('allows any combination of properties', () => {
|
|
15
|
+
expect(() => declareCustomer({ firstName: 'Ziggy', lastName: 'Zebra' })).not.toThrow()
|
|
16
|
+
expect(() => declareCustomer({ firstName: 'Ziggy', email: 'ziggy.zebra@fatzebra.com' })).not.toThrow()
|
|
17
|
+
expect(() => declareCustomer({ address: '2/58-62 Kippax Street', city: 'Surry Hills' })).not.toThrow()
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
it('allows all properties', () => {
|
|
21
|
+
const properties = {
|
|
22
|
+
firstName: 'Ziggy',
|
|
23
|
+
lastName: 'Zebra',
|
|
24
|
+
email: 'ziggy.zebra@fatzebra.com',
|
|
25
|
+
address: '2/58-62 Kippax Street',
|
|
26
|
+
city: 'Surry Hills',
|
|
27
|
+
postcode: '2010',
|
|
28
|
+
state: 'NSW',
|
|
29
|
+
country: 'Australia'
|
|
30
|
+
}
|
|
31
|
+
expect(() => declareCustomer(properties)).not.toThrow()
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
it('allows only first name', () => {
|
|
35
|
+
expect(() => declareCustomer({ firstName: 'Ziggy' })).not.toThrow()
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
it('allows only last name', () => {
|
|
39
|
+
expect(() => declareCustomer({ lastName: 'Zebra' })).not.toThrow()
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
it('allows only email', () => {
|
|
43
|
+
expect(() => declareCustomer({ email: 'ziggy.zebra@fatzebra.com' })).not.toThrow()
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
it('allows only address', () => {
|
|
47
|
+
expect(() => declareCustomer({ address: '2/58-62 Kippax Street' })).not.toThrow()
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
it('allows only city', () => {
|
|
51
|
+
expect(() => declareCustomer({ city: 'Surry Hills' })).not.toThrow()
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
it('allows only postcode', () => {
|
|
55
|
+
expect(() => declareCustomer({ postcode: '2010' })).not.toThrow()
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
it('allows only state', () => {
|
|
59
|
+
expect(() => declareCustomer({ state: 'NSW' })).not.toThrow()
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
it('allows only country', () => {
|
|
63
|
+
expect(() => declareCustomer({ country: 'Australia' })).not.toThrow()
|
|
64
|
+
})
|
|
65
|
+
})
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
|
|
2
|
+
enum NODE_ENV {
|
|
3
|
+
LOCALDEV = 'localdev',
|
|
4
|
+
PRODUCTION = 'production',
|
|
5
|
+
SANDBOX = 'sandbox',
|
|
6
|
+
STAGING = 'staging',
|
|
7
|
+
TEST = 'test',
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
enum PublicEvent {
|
|
11
|
+
FORM_VALIDATION_ERROR = "fz.form_validation.error",
|
|
12
|
+
FORM_VALIDATION_SUCCESS = "fz.form_validation.success",
|
|
13
|
+
VALIDATION_ERROR = "fz.validation.error",
|
|
14
|
+
SCA_SUCCESS = "fz.sca.success",
|
|
15
|
+
SCA_ERROR = "fz.sca.error",
|
|
16
|
+
TOKENIZATION_SUCCESS = "fz.tokenization.success",
|
|
17
|
+
TOKENIZATION_ERROR = "fz.tokenization.error",
|
|
18
|
+
PAYMENT_SUCCESS = "fz.payment.success",
|
|
19
|
+
PAYMENT_ERROR = "fz.payment.error",
|
|
20
|
+
BIN_LOOKUP = "fz.bin.lookup",
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
enum BridgeEvent {
|
|
24
|
+
TOKENIZE_CARD_REQUEST = "fzi.tc_req",
|
|
25
|
+
TOKENIZE_CARD_RESPONSE = "fzi.tc_res",
|
|
26
|
+
CREATE_PAYMENT_REQUEST = "fzi.cp_req",
|
|
27
|
+
CREATE_PAYMENT_RESPONSE = "fzi.cp_res",
|
|
28
|
+
TRIGGER_PAYNOW_BUTTON = "fzi.pn_btn",
|
|
29
|
+
FORM_VALIDATION_ERROR = "fzi.form_validation_error",
|
|
30
|
+
FORM_VALIDATION_SUCCESS = "fzi.form_validation_success",
|
|
31
|
+
BIN_LOOKUP = "fzi.bin_lookup",
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
enum PaymentMethodType {
|
|
35
|
+
CARD = 'card',
|
|
36
|
+
CARD_ON_FILE = 'card_on_file',
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
interface Card {
|
|
40
|
+
number: string
|
|
41
|
+
holder: string
|
|
42
|
+
expiryMonth: string
|
|
43
|
+
expiryYear: string
|
|
44
|
+
cvv: string
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
interface CardOnFile {
|
|
48
|
+
token: string
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
interface Customer {
|
|
52
|
+
firstName?: string
|
|
53
|
+
lastName?: string
|
|
54
|
+
email?: string
|
|
55
|
+
address?: string
|
|
56
|
+
city?: string
|
|
57
|
+
postcode?: string
|
|
58
|
+
state?: string
|
|
59
|
+
country?: string
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
interface CustomerSnakeCase {
|
|
63
|
+
first_name?: string
|
|
64
|
+
last_name?: string
|
|
65
|
+
email?: string
|
|
66
|
+
address?: string
|
|
67
|
+
city?: string
|
|
68
|
+
state?: string
|
|
69
|
+
postcode?: string
|
|
70
|
+
country?: string
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
interface Payment {
|
|
74
|
+
amount: number
|
|
75
|
+
currency: string
|
|
76
|
+
reference: string,
|
|
77
|
+
hide_card_holder?: boolean;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
interface PaymentIntent {
|
|
81
|
+
payment: Payment
|
|
82
|
+
verification: string // hamc_md5 of payment object
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
interface PaymentMethod {
|
|
86
|
+
type?: PaymentMethodType
|
|
87
|
+
data: Card | CardOnFile
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
interface ValidationResult {
|
|
91
|
+
valid: boolean
|
|
92
|
+
errors: string[]
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
type TokenizeCardResponse = {
|
|
96
|
+
bin: string;
|
|
97
|
+
token: string;
|
|
98
|
+
errors?: Array<string>;
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
type Callback = (event: any) => void;
|
|
102
|
+
|
|
103
|
+
type Handlers = Partial<Record<PublicEvent, Callback>>;
|
|
104
|
+
|
|
105
|
+
export type VerificationConfig = {
|
|
106
|
+
accessToken: string;
|
|
107
|
+
paymentIntent: PaymentIntent,
|
|
108
|
+
verification: string;
|
|
109
|
+
options?: Options;
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
export type PaymentConfig = {
|
|
113
|
+
accessToken: string;
|
|
114
|
+
payment: PaymentIntent;
|
|
115
|
+
options: Options;
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
type Options = Partial<{
|
|
119
|
+
css: string;
|
|
120
|
+
cssSignature: string;
|
|
121
|
+
enableSca: boolean;
|
|
122
|
+
tokenizeOnly: boolean;
|
|
123
|
+
}>;
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
export type TransactionPayload = {
|
|
127
|
+
transactionId: string;
|
|
128
|
+
responseCode: string;
|
|
129
|
+
message: string;
|
|
130
|
+
amount: number;
|
|
131
|
+
currency: string;
|
|
132
|
+
reference: string;
|
|
133
|
+
cardNumber: string;
|
|
134
|
+
cardHolder: string;
|
|
135
|
+
cardExpiry: string;
|
|
136
|
+
cardType: string;
|
|
137
|
+
verification: string;
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
export type CreatePaymentResponse = {
|
|
141
|
+
transaction_id: TransactionPayload["transactionId"];
|
|
142
|
+
response_code: TransactionPayload["responseCode"];
|
|
143
|
+
message: TransactionPayload["message"];
|
|
144
|
+
amount: TransactionPayload["amount"];
|
|
145
|
+
currency: TransactionPayload["currency"];
|
|
146
|
+
reference: TransactionPayload["reference"];
|
|
147
|
+
card_number: TransactionPayload["cardNumber"];
|
|
148
|
+
card_holder: TransactionPayload["cardHolder"];
|
|
149
|
+
card_expiry: TransactionPayload["cardExpiry"];
|
|
150
|
+
card_type: TransactionPayload["cardType"];
|
|
151
|
+
v: TransactionPayload["verification"];
|
|
152
|
+
errors?: Array<string>;
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
declare global {
|
|
157
|
+
interface Window {
|
|
158
|
+
ApplePaySession: any
|
|
159
|
+
Cardinal: any
|
|
160
|
+
FatZebra: any
|
|
161
|
+
HPP: any
|
|
162
|
+
MerchantUsername: string
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
export {
|
|
167
|
+
BridgeEvent,
|
|
168
|
+
PublicEvent,
|
|
169
|
+
NODE_ENV,
|
|
170
|
+
Card,
|
|
171
|
+
CardOnFile,
|
|
172
|
+
Customer,
|
|
173
|
+
CustomerSnakeCase,
|
|
174
|
+
Handlers,
|
|
175
|
+
Payment,
|
|
176
|
+
PaymentIntent,
|
|
177
|
+
PaymentMethod,
|
|
178
|
+
PaymentMethodType,
|
|
179
|
+
TokenizeCardResponse,
|
|
180
|
+
|
|
181
|
+
ValidationResult
|
|
182
|
+
}
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import {
|
|
2
|
+
toCamelCase,
|
|
3
|
+
toSnakeCase,
|
|
4
|
+
toObjectWithCamelCaseKeys,
|
|
5
|
+
toObjectWithSnakeCaseKeys,
|
|
6
|
+
} from './util'
|
|
7
|
+
|
|
8
|
+
const objectWithCamelCaseKeys: any = {
|
|
9
|
+
stringKey: 'asdf',
|
|
10
|
+
nestedKey: {
|
|
11
|
+
firstName: 'John',
|
|
12
|
+
lastName: 'Doe',
|
|
13
|
+
nestedArray: [1, 2, 3],
|
|
14
|
+
nestedArray2: [
|
|
15
|
+
{ itemName: 'a1', itemDesc: { partDesc: 'p1' }, subParts: [ {unitCost: 100}, {unitCost: 200} ] },
|
|
16
|
+
{ itemName: 'a2', itemDesc: { partDesc: 'p2' }, subParts: [ {unitCost: 300}, {unitCost: 400} ] },
|
|
17
|
+
]
|
|
18
|
+
},
|
|
19
|
+
arrayKey: ['asd', 'f12'],
|
|
20
|
+
nullKey: null,
|
|
21
|
+
undefinedKey: undefined,
|
|
22
|
+
emptyKey: '',
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const objectWithSnakeCaseKeys: any = {
|
|
26
|
+
string_key: 'asdf',
|
|
27
|
+
nested_key: {
|
|
28
|
+
first_name: 'John',
|
|
29
|
+
last_name: 'Doe',
|
|
30
|
+
nested_array: [1, 2, 3],
|
|
31
|
+
nested_array2: [
|
|
32
|
+
{ item_name: 'a1', item_desc: { part_desc: 'p1' }, sub_parts: [ {unit_cost: 100}, {unit_cost: 200} ] },
|
|
33
|
+
{ item_name: 'a2', item_desc: { part_desc: 'p2' }, sub_parts: [ {unit_cost: 300}, {unit_cost: 400} ] },
|
|
34
|
+
]
|
|
35
|
+
},
|
|
36
|
+
array_key: ['asd', 'f12'],
|
|
37
|
+
null_key: null,
|
|
38
|
+
undefined_key: undefined,
|
|
39
|
+
empty_key: '',
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
describe('toCamelCase', () => {
|
|
43
|
+
it('Convert snakecase string to camelcase string', () => {
|
|
44
|
+
const subject = 'snake_case'
|
|
45
|
+
const actual = toCamelCase(subject)
|
|
46
|
+
const expected = 'snakeCase'
|
|
47
|
+
expect(actual).toEqual(expected)
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
it('Leaves camelcase string intact', () => {
|
|
51
|
+
const subject = 'camelCase'
|
|
52
|
+
const actual = toCamelCase(subject)
|
|
53
|
+
const expected = 'camelCase'
|
|
54
|
+
expect(actual).toEqual(expected)
|
|
55
|
+
})
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
describe('toSnakeCase', () => {
|
|
59
|
+
it('Convert camelcase string to snakecase string', () => {
|
|
60
|
+
const subject = 'camelCase'
|
|
61
|
+
const actual = toSnakeCase(subject)
|
|
62
|
+
const expected = 'camel_case'
|
|
63
|
+
expect(actual).toEqual(expected)
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
it('Leaves snakecase string intact', () => {
|
|
67
|
+
const subject = 'snake_case'
|
|
68
|
+
const actual = toSnakeCase(subject)
|
|
69
|
+
const expected = 'snake_case'
|
|
70
|
+
expect(actual).toEqual(expected)
|
|
71
|
+
})
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
describe('toObjectWithCamelCaseKeys', () => {
|
|
75
|
+
it ('Converts object with snake case keys to camel case', () => {
|
|
76
|
+
const actual = toObjectWithCamelCaseKeys(objectWithSnakeCaseKeys)
|
|
77
|
+
const expected: any = {
|
|
78
|
+
stringKey: 'asdf',
|
|
79
|
+
nestedKey: {
|
|
80
|
+
firstName: 'John',
|
|
81
|
+
lastName: 'Doe',
|
|
82
|
+
nestedArray: [1, 2, 3],
|
|
83
|
+
nestedArray2: [
|
|
84
|
+
{ itemName: 'a1', itemDesc: { partDesc: 'p1' }, subParts: [ {unitCost: 100}, {unitCost: 200} ] },
|
|
85
|
+
{ itemName: 'a2', itemDesc: { partDesc: 'p2' }, subParts: [ {unitCost: 300}, {unitCost: 400} ] },
|
|
86
|
+
]
|
|
87
|
+
},
|
|
88
|
+
arrayKey: ['asd', 'f12'],
|
|
89
|
+
nullKey: null,
|
|
90
|
+
undefinedKey: undefined,
|
|
91
|
+
emptyKey: '',
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
expect(actual).toMatchObject(expected)
|
|
95
|
+
})
|
|
96
|
+
|
|
97
|
+
it ('Leave object with camel case intact', () => {
|
|
98
|
+
const actual = toObjectWithCamelCaseKeys(objectWithCamelCaseKeys)
|
|
99
|
+
const expected: any = {
|
|
100
|
+
stringKey: 'asdf',
|
|
101
|
+
nestedKey: {
|
|
102
|
+
firstName: 'John',
|
|
103
|
+
lastName: 'Doe',
|
|
104
|
+
nestedArray: [1, 2, 3],
|
|
105
|
+
nestedArray2: [
|
|
106
|
+
{ itemName: 'a1', itemDesc: { partDesc: 'p1' }, subParts: [ {unitCost: 100}, {unitCost: 200} ] },
|
|
107
|
+
{ itemName: 'a2', itemDesc: { partDesc: 'p2' }, subParts: [ {unitCost: 300}, {unitCost: 400} ] },
|
|
108
|
+
]
|
|
109
|
+
},
|
|
110
|
+
arrayKey: ['asd', 'f12'],
|
|
111
|
+
nullKey: null,
|
|
112
|
+
undefinedKey: undefined,
|
|
113
|
+
emptyKey: '',
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
expect(actual).toMatchObject(expected)
|
|
117
|
+
})
|
|
118
|
+
})
|
|
119
|
+
|
|
120
|
+
describe('toObjectWithSnakeCaseKeys', () => {
|
|
121
|
+
it ('Converts object with camel case keys to snake case', () => {
|
|
122
|
+
const actual = toObjectWithSnakeCaseKeys(objectWithCamelCaseKeys)
|
|
123
|
+
const expected: any = {
|
|
124
|
+
string_key: 'asdf',
|
|
125
|
+
nested_key: {
|
|
126
|
+
first_name: 'John',
|
|
127
|
+
last_name: 'Doe',
|
|
128
|
+
nested_array: [1, 2, 3],
|
|
129
|
+
nested_array2: [
|
|
130
|
+
{ item_name: 'a1', item_desc: { part_desc: 'p1' }, sub_parts: [ {unit_cost: 100}, {unit_cost: 200} ] },
|
|
131
|
+
{ item_name: 'a2', item_desc: { part_desc: 'p2' }, sub_parts: [ {unit_cost: 300}, {unit_cost: 400} ] },
|
|
132
|
+
]
|
|
133
|
+
},
|
|
134
|
+
array_key: ['asd', 'f12'],
|
|
135
|
+
null_key: null,
|
|
136
|
+
undefined_key: undefined,
|
|
137
|
+
empty_key: '',
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
expect(actual).toMatchObject(expected)
|
|
141
|
+
})
|
|
142
|
+
|
|
143
|
+
it ('Leave object with snake case intact', () => {
|
|
144
|
+
const actual = toObjectWithSnakeCaseKeys(objectWithSnakeCaseKeys)
|
|
145
|
+
const expected: any = {
|
|
146
|
+
string_key: 'asdf',
|
|
147
|
+
nested_key: {
|
|
148
|
+
first_name: 'John',
|
|
149
|
+
last_name: 'Doe',
|
|
150
|
+
nested_array: [1, 2, 3],
|
|
151
|
+
nested_array2: [
|
|
152
|
+
{ item_name: 'a1', item_desc: { part_desc: 'p1' }, sub_parts: [ {unit_cost: 100}, {unit_cost: 200} ] },
|
|
153
|
+
{ item_name: 'a2', item_desc: { part_desc: 'p2' }, sub_parts: [ {unit_cost: 300}, {unit_cost: 400} ] },
|
|
154
|
+
]
|
|
155
|
+
},
|
|
156
|
+
array_key: ['asd', 'f12'],
|
|
157
|
+
null_key: null,
|
|
158
|
+
undefined_key: undefined,
|
|
159
|
+
empty_key: '',
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
expect(actual).toMatchObject(expected)
|
|
163
|
+
})
|
|
164
|
+
})
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
const _package = require('../../package.json')
|
|
2
|
+
|
|
3
|
+
const toCamelCase = (s: string): string => {
|
|
4
|
+
return s.replace(/_([a-z])/g, (g) => g[1].toUpperCase());
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
const toSnakeCase = (s: string): string => {
|
|
8
|
+
return s.replace(/[A-Z]/g, (char) => `_${char.toLowerCase()}`)
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const primitiveTypes = ['string', 'number', 'boolean', 'undefined', 'symbol', 'bigint']
|
|
12
|
+
|
|
13
|
+
const toObjectWithSnakeCaseKeys = (o: any): any => {
|
|
14
|
+
let objectType = typeof o
|
|
15
|
+
if (primitiveTypes.includes(objectType) || o == null){
|
|
16
|
+
return o
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
let converted: any = {}
|
|
20
|
+
|
|
21
|
+
for (let key in o) {
|
|
22
|
+
switch (typeof o[key]) {
|
|
23
|
+
case 'object':
|
|
24
|
+
if (Array.isArray(o[key])) {
|
|
25
|
+
converted[toSnakeCase(key)] = []
|
|
26
|
+
Object.keys(o[key]).forEach(function (k) {
|
|
27
|
+
let c = toObjectWithSnakeCaseKeys(o[key][k])
|
|
28
|
+
converted[toSnakeCase(key)].push(c)
|
|
29
|
+
})
|
|
30
|
+
} else {
|
|
31
|
+
if (o[key] == null) {
|
|
32
|
+
converted[toSnakeCase(key)] = o[key]
|
|
33
|
+
} else {
|
|
34
|
+
converted[toSnakeCase(key)] = toObjectWithSnakeCaseKeys(o[key])
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
break
|
|
38
|
+
case 'string':
|
|
39
|
+
case 'number':
|
|
40
|
+
case 'boolean':
|
|
41
|
+
case 'undefined':
|
|
42
|
+
converted[toSnakeCase(key)] = o[key]
|
|
43
|
+
break
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return converted
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const toObjectWithCamelCaseKeys = (o: any): any => {
|
|
51
|
+
let objectType = typeof o
|
|
52
|
+
if (primitiveTypes.includes(objectType) || o == null){
|
|
53
|
+
return o
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
let converted: any = {}
|
|
57
|
+
|
|
58
|
+
for (let key in o) {
|
|
59
|
+
switch (typeof o[key]) {
|
|
60
|
+
case 'object':
|
|
61
|
+
if (Array.isArray(o[key])) {
|
|
62
|
+
converted[toCamelCase(key)] = []
|
|
63
|
+
Object.keys(o[key]).forEach(function (k) {
|
|
64
|
+
let c = toObjectWithCamelCaseKeys(o[key][k])
|
|
65
|
+
converted[toCamelCase(key)].push(c)
|
|
66
|
+
})
|
|
67
|
+
} else {
|
|
68
|
+
if (o[key] == null) {
|
|
69
|
+
converted[toCamelCase(key)] = o[key]
|
|
70
|
+
} else {
|
|
71
|
+
converted[toCamelCase(key)] = toObjectWithCamelCaseKeys(o[key])
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
break
|
|
75
|
+
case 'string':
|
|
76
|
+
case 'number':
|
|
77
|
+
case 'boolean':
|
|
78
|
+
case 'undefined':
|
|
79
|
+
converted[toCamelCase(key)] = o[key]
|
|
80
|
+
break
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
return converted
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
const getSdkVersionNumber = () => {
|
|
89
|
+
return _package.version
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export {
|
|
93
|
+
toCamelCase,
|
|
94
|
+
toSnakeCase,
|
|
95
|
+
toObjectWithCamelCaseKeys,
|
|
96
|
+
toObjectWithSnakeCaseKeys,
|
|
97
|
+
getSdkVersionNumber,
|
|
98
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import * as nock from 'nock'
|
|
2
|
+
|
|
3
|
+
const apiGatewayUrl = process.env.API_GATEWAY_URL
|
|
4
|
+
|
|
5
|
+
interface MockRequestConfig {
|
|
6
|
+
request: {
|
|
7
|
+
action: string,
|
|
8
|
+
path: string,
|
|
9
|
+
body: any
|
|
10
|
+
},
|
|
11
|
+
response: {
|
|
12
|
+
status: number,
|
|
13
|
+
body: any
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const mockRequest = (config: MockRequestConfig) => {
|
|
18
|
+
nock(apiGatewayUrl)
|
|
19
|
+
.defaultReplyHeaders({
|
|
20
|
+
'Access-Control-Allow-Origin': '*',
|
|
21
|
+
'Access-Control-Allow-Headers': 'Authorization, FZ-SDK-Version, fz-merchant-username',
|
|
22
|
+
"Content-Type": "application:json"
|
|
23
|
+
})
|
|
24
|
+
.options(config.request.path)
|
|
25
|
+
.reply(200, null)
|
|
26
|
+
.post(config.request.path, config.request.body)
|
|
27
|
+
.reply(config.response.status, config.response.body)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const mockCustomRequest = (config: MockRequestConfig) => {
|
|
31
|
+
nock(apiGatewayUrl)
|
|
32
|
+
.defaultReplyHeaders({
|
|
33
|
+
'Access-Control-Allow-Origin': '*',
|
|
34
|
+
'Access-Control-Allow-Headers': 'Authorization',
|
|
35
|
+
"Content-Type": "application:json"
|
|
36
|
+
})
|
|
37
|
+
.options(config.request.path)
|
|
38
|
+
.reply(200, null)
|
|
39
|
+
.intercept(config.request.path, config.request.action, config.request.body)
|
|
40
|
+
.reply(config.response.status, config.response.body)
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export {
|
|
44
|
+
mockRequest,
|
|
45
|
+
mockCustomRequest,
|
|
46
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"declaration": true,
|
|
4
|
+
"outDir": "./dist/",
|
|
5
|
+
"jsx": "react",
|
|
6
|
+
"esModuleInterop": true,
|
|
7
|
+
"sourceMap": true,
|
|
8
|
+
"noImplicitAny": true,
|
|
9
|
+
"module": "commonjs",
|
|
10
|
+
"target": "es5",
|
|
11
|
+
"lib": [
|
|
12
|
+
"es2019"
|
|
13
|
+
]
|
|
14
|
+
},
|
|
15
|
+
"include": [
|
|
16
|
+
"./src/**/*"
|
|
17
|
+
],
|
|
18
|
+
"exclude": [
|
|
19
|
+
"node_modules",
|
|
20
|
+
"**/*.test.ts"
|
|
21
|
+
]
|
|
22
|
+
}
|