-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
710 additions
and
12 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export const WebhookEvent = { | ||
order_status_update: "order_status_update", | ||
template_preview_rendered: "template_preview_rendered", | ||
} as const; | ||
|
||
export type WebhookEvent = (typeof WebhookEvent)[keyof typeof WebhookEvent]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { Protected } from "~/PrintOne"; | ||
import { IWebhook } from "~/models/_interfaces/IWebhook"; | ||
import { WebhookLog } from "~/models/WebhookLog"; | ||
import { IWebhookLog } from "~/models/_interfaces/IWebhookLog"; | ||
import { WebhookEvent } from "~/enums/WebhookEvent"; | ||
import { PaginatedResponse } from "~/models/PaginatedResponse"; | ||
import { IPaginatedResponse } from "~/models/_interfaces/IPaginatedResponse"; | ||
|
||
export class Webhook { | ||
private _data: IWebhook; | ||
|
||
constructor( | ||
private readonly _protected: Protected, | ||
_data: IWebhook, | ||
) { | ||
this._data = _data; | ||
} | ||
|
||
public get id(): string { | ||
return this._data.id; | ||
} | ||
|
||
public get name(): string { | ||
return this._data.name; | ||
} | ||
|
||
public get events(): WebhookEvent[] { | ||
return this._data.events; | ||
} | ||
|
||
public get active(): boolean { | ||
return this._data.active; | ||
} | ||
|
||
public get headers(): Record<string, string> { | ||
return this._data.headers; | ||
} | ||
|
||
public get secretHeaders(): Record<string, string> { | ||
return this._data.secretHeaders; | ||
} | ||
|
||
public get url(): string { | ||
return this._data.url; | ||
} | ||
|
||
public get successRate(): number | null { | ||
return this._data.successRate; | ||
} | ||
|
||
public async update(data: Partial<Omit<IWebhook, "id">>): Promise<void> { | ||
this._data = await this._protected.client.PATCH<IWebhook>( | ||
`/webhooks/${this.id}`, | ||
data, | ||
); | ||
} | ||
|
||
public async delete(): Promise<void> { | ||
await this._protected.client.DELETE<void>(`/webhooks/${this.id}`); | ||
} | ||
|
||
public async getLogs(): Promise<PaginatedResponse<WebhookLog>> { | ||
const logs = await this._protected.client.GET< | ||
IPaginatedResponse<IWebhookLog> | ||
>(`/webhooks/${this.id}/logs`); | ||
|
||
return PaginatedResponse.safe( | ||
this._protected, | ||
logs, | ||
(log) => new WebhookLog(this._protected, log), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { Protected } from "~/PrintOne"; | ||
import { | ||
IWebhookLog, | ||
IWebhookLogResponse, | ||
} from "~/models/_interfaces/IWebhookLog"; | ||
import { WebhookEvent } from "~/enums/WebhookEvent"; | ||
import { WebhookRequest, webhookRequestFactory } from "~/models/WebhookRequest"; | ||
|
||
export class WebhookLog { | ||
constructor( | ||
private readonly _protected: Protected, | ||
private _data: IWebhookLog, | ||
) {} | ||
|
||
public get id(): string { | ||
return this._data.id; | ||
} | ||
|
||
public get status(): "success" | "failed" { | ||
return this._data.status; | ||
} | ||
|
||
public get event(): WebhookEvent { | ||
return this._data.event; | ||
} | ||
|
||
public get request(): WebhookRequest { | ||
return webhookRequestFactory(this._protected, this._data.request); | ||
} | ||
|
||
public get response(): IWebhookLogResponse { | ||
return this._data.response; | ||
} | ||
|
||
public get createdAt(): Date { | ||
return new Date(this._data.createdAt); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { | ||
IOrderStatusUpdateWebhookRequest, | ||
ITemplatePreviewRenderedWebhookRequest, | ||
IWebhookRequest, | ||
} from "~/models/_interfaces/IWebhookRequest"; | ||
import { Protected } from "~/PrintOne"; | ||
import { Order } from "~/models/Order"; | ||
import { PreviewDetails } from "~/models/PreviewDetails"; | ||
|
||
abstract class AbstractWebhookRequest<T, E extends IWebhookRequest> { | ||
constructor( | ||
protected readonly _protected: Protected, | ||
protected _data: E, | ||
) {} | ||
|
||
abstract data: T; | ||
|
||
get event(): E["event"] { | ||
return this._data.event; | ||
} | ||
|
||
get createdAt(): Date { | ||
return new Date(this._data.createdAt); | ||
} | ||
} | ||
|
||
export type WebhookRequest = | ||
| OrderStatusUpdateWebhookRequest | ||
| TemplatePreviewRenderedWebhookRequest; | ||
|
||
export function webhookRequestFactory( | ||
_protected: Protected, | ||
data: IWebhookRequest, | ||
): WebhookRequest { | ||
const event = data.event; | ||
|
||
switch (event) { | ||
case "order_status_update": | ||
return new OrderStatusUpdateWebhookRequest(_protected, data); | ||
case "template_preview_rendered": | ||
return new TemplatePreviewRenderedWebhookRequest(_protected, data); | ||
default: | ||
throw new Error(`Unknown webhook event: ${event}`); | ||
} | ||
} | ||
|
||
export class OrderStatusUpdateWebhookRequest extends AbstractWebhookRequest< | ||
Order, | ||
IOrderStatusUpdateWebhookRequest | ||
> { | ||
get data(): Order { | ||
return new Order(this._protected, this._data.data); | ||
} | ||
} | ||
|
||
export class TemplatePreviewRenderedWebhookRequest extends AbstractWebhookRequest< | ||
PreviewDetails, | ||
ITemplatePreviewRenderedWebhookRequest | ||
> { | ||
get data(): PreviewDetails { | ||
return new PreviewDetails(this._protected, this._data.data); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,5 @@ export type IPreviewDetails = { | |
id: string; | ||
errors: string[]; | ||
imageUrl: string; | ||
templateId: string; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { WebhookEvent } from "~/enums/WebhookEvent"; | ||
|
||
export type IWebhook = { | ||
id: string; | ||
name: string; | ||
events: WebhookEvent[]; | ||
active: boolean; | ||
headers: Record<string, string>; | ||
secretHeaders: Record<string, string>; | ||
url: string; | ||
successRate: number | null; | ||
}; | ||
|
||
export type CreateWebhook = Omit< | ||
IWebhook, | ||
"id" | "headers" | "secretHeaders" | "successRate" | ||
> & { | ||
headers?: Record<string, string>; | ||
secretHeaders?: Record<string, string>; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { WebhookEvent } from "~/enums/WebhookEvent"; | ||
import { IWebhookRequest } from "~/models/_interfaces/IWebhookRequest"; | ||
|
||
export type IWebhookLog = { | ||
id: string; | ||
status: "success" | "failed"; | ||
event: WebhookEvent; | ||
request: IWebhookRequest; | ||
response: IWebhookLogResponse; | ||
createdAt: string; | ||
}; | ||
|
||
export type IWebhookLogResponse = { | ||
status: number; | ||
body: string; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { IOrder } from "~/models/_interfaces/IOrder"; | ||
import { IPreviewDetails } from "~/models/_interfaces/IPreviewDetails"; | ||
|
||
export type IWebhookRequest = | ||
| IOrderStatusUpdateWebhookRequest | ||
| ITemplatePreviewRenderedWebhookRequest; | ||
|
||
export type IOrderStatusUpdateWebhookRequest = { | ||
data: IOrder; | ||
event: "order_status_update"; | ||
createdAt: string; | ||
}; | ||
|
||
export type ITemplatePreviewRenderedWebhookRequest = { | ||
data: IPreviewDetails; | ||
event: "template_preview_rendered"; | ||
createdAt: string; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.