From 91905bfacb4fb81b7ed9b18ec9432247f153f68c Mon Sep 17 00:00:00 2001 From: Teerawat Lamanchart Date: Wed, 11 Aug 2021 01:24:37 +0700 Subject: [PATCH] Initial commit --- .vscode/settings.json | 7 +++++++ README.md | 12 +++++++++++ lib/monoova_client.ts | 37 ++++++++++++++++++++++++++++++++++ scripts/_helpers.ts | 37 ++++++++++++++++++++++++++++++++++ scripts/list_webhooks.ts | 8 ++++++++ scripts/ping.ts | 7 +++++++ scripts/subscribe_webhook.ts | 15 ++++++++++++++ scripts/unsubscribe_webhook.ts | 10 +++++++++ scripts/validate_bsb.ts | 10 +++++++++ 9 files changed, 143 insertions(+) create mode 100644 .vscode/settings.json create mode 100644 README.md create mode 100644 lib/monoova_client.ts create mode 100644 scripts/_helpers.ts create mode 100644 scripts/list_webhooks.ts create mode 100644 scripts/ping.ts create mode 100644 scripts/subscribe_webhook.ts create mode 100644 scripts/unsubscribe_webhook.ts create mode 100644 scripts/validate_bsb.ts diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..1ffa96f --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,7 @@ +{ + "deno.enable": true, + "deno.lint": true, + "[typescript]": { + "editor.defaultFormatter": "denoland.vscode-deno" + } +} diff --git a/README.md b/README.md new file mode 100644 index 0000000..84477f0 --- /dev/null +++ b/README.md @@ -0,0 +1,12 @@ +# Monoova + +## Prerequisite + +Make sure you have [deno](https://deno.land/#installation) installed. + +## Usage + +```sh +$ export MONOOVA_API_KEY=xxx +$ deno run --allow-net --allow-env scripts/ping.ts +``` diff --git a/lib/monoova_client.ts b/lib/monoova_client.ts new file mode 100644 index 0000000..5991db0 --- /dev/null +++ b/lib/monoova_client.ts @@ -0,0 +1,37 @@ +export type Client = { + apiKey: string; + baseUrl: string; +}; + +export function newSandboxClient(apiKey: string): Client { + return { + apiKey: apiKey, + baseUrl: "https://api.m-pay.com.au", + }; +} + +export function newProductionClient(apiKey: string): Client { + return { + apiKey: apiKey, + baseUrl: "https://api.mpay.com.au", + }; +} + +export async function request( + client: Client, + method: string, + path: string, + body?: Record, +): Promise> { + const url = client.baseUrl + path; + const headers = new Headers({ + "Authorization": "Basic " + btoa(client.apiKey + ":"), + "Content-Type": "application/json", + }); + const response = await fetch(url, { + method, + headers, + body: JSON.stringify(body), + }); + return response.json(); +} diff --git a/scripts/_helpers.ts b/scripts/_helpers.ts new file mode 100644 index 0000000..91a13b1 --- /dev/null +++ b/scripts/_helpers.ts @@ -0,0 +1,37 @@ +import { + Client, + newProductionClient, + newSandboxClient, +} from "../lib/monoova_client.ts"; + +function getAPIKey(): string { + const apiKey: string | undefined = Deno.env.get("MONOOVA_API_KEY"); + + if (apiKey === undefined) { + console.error("MONOOVA_API_KEY is missing!"); + Deno.exit(1); + } + + return apiKey; +} + +export function newClient(): Client { + const apiKey = getAPIKey(); + + const env = getUserInput( + "Please select environment:\n1. sandbox\n2. production\n>>", + ); + + if (env === "1") { + return newSandboxClient(apiKey); + } else if (env === "2") { + return newProductionClient(apiKey); + } else { + Deno.exit(1); + } +} + +export function getUserInput(message: string): string { + const input = prompt(message) || ""; + return input.trim(); +} diff --git a/scripts/list_webhooks.ts b/scripts/list_webhooks.ts new file mode 100644 index 0000000..cb9abaa --- /dev/null +++ b/scripts/list_webhooks.ts @@ -0,0 +1,8 @@ +import { newClient } from "./_helpers.ts"; +import { request } from "../lib/monoova_client.ts"; + +const client = newClient(); + +const json = await request(client, "GET", "/subscriptions/v1/list"); + +console.log(json); diff --git a/scripts/ping.ts b/scripts/ping.ts new file mode 100644 index 0000000..743a917 --- /dev/null +++ b/scripts/ping.ts @@ -0,0 +1,7 @@ +import { newClient } from "./_helpers.ts"; +import { request } from "../lib/monoova_client.ts"; + +const client = newClient(); +const json = await request(client, "GET", "/tools/v1/ping"); + +console.log(json); diff --git a/scripts/subscribe_webhook.ts b/scripts/subscribe_webhook.ts new file mode 100644 index 0000000..40ca60f --- /dev/null +++ b/scripts/subscribe_webhook.ts @@ -0,0 +1,15 @@ +import { getUserInput, newClient } from "./_helpers.ts"; +import { request } from "../lib/monoova_client.ts"; + +const client = newClient(); + +const eventName = getUserInput("event name:"); +const targetUrl = getUserInput("target url:"); + +const json = await request(client, "POST", "/subscriptions/v1/create", { + "eventName": eventName, + "targetUrl": targetUrl, + "subscriptionStatus": "On", +}); + +console.log(json); diff --git a/scripts/unsubscribe_webhook.ts b/scripts/unsubscribe_webhook.ts new file mode 100644 index 0000000..9416be8 --- /dev/null +++ b/scripts/unsubscribe_webhook.ts @@ -0,0 +1,10 @@ +import { getUserInput, newClient } from "./_helpers.ts"; +import { request } from "../lib/monoova_client.ts"; + +const client = newClient(); + +const id = getUserInput("id:"); + +const json = await request(client, "DELETE", "/subscriptions/v1/delete/" + id); + +console.log(json); diff --git a/scripts/validate_bsb.ts b/scripts/validate_bsb.ts new file mode 100644 index 0000000..409d9f9 --- /dev/null +++ b/scripts/validate_bsb.ts @@ -0,0 +1,10 @@ +import { getUserInput, newClient } from "./_helpers.ts"; +import { request } from "../lib/monoova_client.ts"; + +const client = newClient(); + +const bsbNumber = getUserInput("bsb number:"); + +const json = await request(client, "GET", "/tools/v1/bsbValidate/" + bsbNumber); + +console.log(json);