feat: implement renewal/complete new

This commit is contained in:
2023-04-17 21:33:55 +01:00
parent c59dda1e49
commit b6cc1765db
4 changed files with 151 additions and 67 deletions

View File

@ -22,6 +22,22 @@ export interface Subscription {
type: SubscriptionType;
created: string;
expires: string;
state: "new" | "expired" | "paid";
}
export enum SubscriptionErrorCode {
InternalError = 1,
SubscriptionActive = 2,
Duplicate = 3,
}
export class SubscriptionError extends Error {
code: SubscriptionErrorCode;
constructor(msg: string, code: SubscriptionErrorCode) {
super(msg);
this.code = code;
}
}
export default class SnortApi {
@ -49,6 +65,10 @@ export default class SnortApi {
return this.#getJsonAuthd<InvoiceResponse>(`api/v1/subscription?type=${type}`, "PUT");
}
renewSubscription(id: string) {
return this.#getJsonAuthd<InvoiceResponse>(`api/v1/subscription/${id}/renew`, "GET");
}
listSubscriptions() {
return this.#getJsonAuthd<Array<Subscription>>("api/v1/subscription");
}
@ -93,7 +113,7 @@ export default class SnortApi {
const obj = await rsp.json();
if ("error" in obj) {
throw new Error(obj.error);
throw new SubscriptionError(obj.error, obj.code);
}
return obj as T;
}