Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
60 changes: 56 additions & 4 deletions package-lock.json

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

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
"url": "https://github.com/chargily/chargily-pay-javascript/issues"
},
"homepage": "https://github.com/chargily/chargily-pay-javascript#readme",
"dependencies": {
"joi": "^17.11.0"
},
"devDependencies": {
"@types/node": "^14.0.0",
"nodemon": "^3.1.0",
Expand Down
37 changes: 21 additions & 16 deletions src/classes/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ import {
UpdateProductParams,
} from '../types/param';
import { DeleteItemResponse, ListResponse } from '../types/response';
import { validatePayload } from '../validation/validator';
import {
createCustomerSchema,
updateCustomerSchema,
createProductSchema,
updateProductSchema,
createPriceSchema,
updatePriceSchema,
createCheckoutSchema,
createPaymentLinkSchema,
updatePaymentLinkSchema,
} from '../validation/schemas';

/**
* Configuration options for ChargilyClient.
Expand Down Expand Up @@ -116,6 +128,7 @@ export class ChargilyClient {
public async createCustomer(
customer_data: CreateCustomerParams
): Promise<Customer> {
await validatePayload(createCustomerSchema, customer_data, 'Create Customer');
return this.request('customers', 'POST', customer_data);
}

Expand All @@ -138,6 +151,7 @@ export class ChargilyClient {
customer_id: string,
update_data: UpdateCustomerParams
): Promise<Customer> {
await validatePayload(updateCustomerSchema, update_data, 'Update Customer');
return this.request(`customers/${customer_id}`, 'PATCH', update_data);
}

Expand Down Expand Up @@ -176,6 +190,7 @@ export class ChargilyClient {
public async createProduct(
product_data: CreateProductParams
): Promise<Product> {
await validatePayload(createProductSchema, product_data, 'Create Product');
return this.request('products', 'POST', product_data);
}

Expand All @@ -189,6 +204,7 @@ export class ChargilyClient {
product_id: string,
update_data: UpdateProductParams
): Promise<Product> {
await validatePayload(updateProductSchema, update_data, 'Update Product');
return this.request(`products/${product_id}`, 'POST', update_data);
}

Expand Down Expand Up @@ -247,6 +263,7 @@ export class ChargilyClient {
* @returns {Promise<Price>} The created price object.
*/
public async createPrice(price_data: CreatePriceParams): Promise<Price> {
await validatePayload(createPriceSchema, price_data, 'Create Price');
return this.request('prices', 'POST', price_data);
}

Expand All @@ -260,6 +277,7 @@ export class ChargilyClient {
price_id: string,
update_data: UpdatePriceParams
): Promise<Price> {
await validatePayload(updatePriceSchema, update_data, 'Update Price');
return this.request(`prices/${price_id}`, 'POST', update_data);
}

Expand Down Expand Up @@ -291,22 +309,7 @@ export class ChargilyClient {
public async createCheckout(
checkout_data: CreateCheckoutParams
): Promise<Checkout> {
if (
!checkout_data.success_url.startsWith('http') &&
!checkout_data.success_url.startsWith('https')
) {
throw new Error('Invalid success_url, it must begin with http or https.');
}

if (
!checkout_data.items &&
(!checkout_data.amount || !checkout_data.currency)
) {
throw new Error(
'The items field is required when amount and currency are not present.'
);
}

await validatePayload(createCheckoutSchema, checkout_data, 'Create Checkout');
return this.request('checkouts', 'POST', checkout_data);
}

Expand Down Expand Up @@ -370,6 +373,7 @@ export class ChargilyClient {
public async createPaymentLink(
payment_link_data: CreatePaymentLinkParams
): Promise<PaymentLink> {
await validatePayload(createPaymentLinkSchema, payment_link_data, 'Create Payment Link');
return this.request('payment-links', 'POST', payment_link_data);
}

Expand All @@ -383,6 +387,7 @@ export class ChargilyClient {
payment_link_id: string,
update_data: UpdatePaymentLinkParams
): Promise<PaymentLink> {
await validatePayload(updatePaymentLinkSchema, update_data, 'Update Payment Link');
return this.request(
`payment-links/${payment_link_id}`,
'POST',
Expand Down
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@ export * from './classes/client';

// Exporting all the utility functions from utils folder
export * from './utils';

// Exporting validation utilities and schemas from validation folder
export * from './validation';
12 changes: 12 additions & 0 deletions src/validation/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export { validatePayload } from './validator';
export {
createCustomerSchema,
updateCustomerSchema,
createProductSchema,
updateProductSchema,
createPriceSchema,
updatePriceSchema,
createCheckoutSchema,
createPaymentLinkSchema,
updatePaymentLinkSchema,
} from './schemas';
Loading