Skip to content

Commit

Permalink
feat: product - fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahdeyyy committed Apr 10, 2024
1 parent 8c3a999 commit 1ad7252
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 4 deletions.
19 changes: 18 additions & 1 deletion lib/product.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import { Paystack } from "./paystack"


const paystack = mock(() => new Paystack(import.meta.env.PAYSTACK_SECRET_KEY ?? ""))
// const customer = mock(async () => await paystack().customer.create({ email: "[email protected]" }))
const product = mock(async () => await paystack().product.create({
name: "mithril", description: "a rare and mythical item", price: 9999999999, currency: "NGN"
})
)
// const blacklist_customer = mock(async () => await paystack().customer.create({ email: "[email protected]" }))

test("create product", async () => {
Expand All @@ -25,3 +28,17 @@ test("list products", async () => {
expect(response.meta.perPage).toBe(10)
}
})

test("fetch a product", async () => {
const pro = await product()
expect(pro.status).toBe(true)
if (pro.status) {
let response = await paystack().product.fetch(pro.data.id)
expect(response.status).toBe(true)
if (response.status) {
expect(response.data.id).toBe(pro.data.id)
expect(response.data.name).toBe(pro.data.name)
expect(response.data.description).toBe(pro.data.description)
}
}
})
15 changes: 12 additions & 3 deletions lib/product.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { CreateProductData, CreateProductResponse, ListProductQuery, ListProductResponse } from "./types";
import type { CreateProductData, CreateProductResponse, FetchProductResponse, ListProductQuery, ListProductResponse } from "./types";

export class Product {
private secret_key: string;
endpoint: string = "https://api.paystack.co/product/";
endpoint: string = "https://api.paystack.co/product";

constructor(secret_key: string) {
this.secret_key = secret_key;
Expand Down Expand Up @@ -46,6 +46,15 @@ export class Product {
}
return response_data;
}
async fetch() { }
async fetch(id: number): Promise<FetchProductResponse> {
const headers = this.get_headers()
const url = `${this.endpoint}/${id}`
const response = await fetch(url, {
method: "GET",
headers: headers,
})
const response_data = await response.json() as FetchProductResponse;
return response_data
}
async update() { }
}
41 changes: 41 additions & 0 deletions lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -913,3 +913,44 @@ export type ListProductResponse = { message: string; } & ({
pageCount: number;
}
} | PaystackResponseError)

export type FetchProductResponse = { message: string; } & ({
status: true;
data: {
digital_assets: any[];
integration: number;
name: string;
description: string;
product_code: string;
price: number;
currency: Currency;
quantity: number;
quantity_sold: number | null;
type: string;
files: any | null;
file_path: any | null;
is_shippable: boolean;
shipping_fields: ShippingFields;
unlimited: boolean;
domain: string;
active: boolean;
features: any | null;
in_stock: boolean;
metadata: {
background_color: string;
};
slug: string;
success_message: string | null;
redirect_url: string | null;
split_code: string | null;
notification_emails: string | null;
minimum_orderable: number;
maximum_orderable: number | null;
low_stock_alert: boolean;
stock_threshold: any | null;
expires_in: any | null;
id: number;
createdAt: string;
updatedAt: string;
}
} | PaystackResponseError)

0 comments on commit 1ad7252

Please sign in to comment.