Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
tteerawat committed Aug 10, 2021
0 parents commit 91905bf
Show file tree
Hide file tree
Showing 9 changed files with 143 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"deno.enable": true,
"deno.lint": true,
"[typescript]": {
"editor.defaultFormatter": "denoland.vscode-deno"
}
}
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
```
37 changes: 37 additions & 0 deletions lib/monoova_client.ts
Original file line number Diff line number Diff line change
@@ -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<string, unknown>,
): Promise<Record<string, unknown>> {
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();
}
37 changes: 37 additions & 0 deletions scripts/_helpers.ts
Original file line number Diff line number Diff line change
@@ -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();
}
8 changes: 8 additions & 0 deletions scripts/list_webhooks.ts
Original file line number Diff line number Diff line change
@@ -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);
7 changes: 7 additions & 0 deletions scripts/ping.ts
Original file line number Diff line number Diff line change
@@ -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);
15 changes: 15 additions & 0 deletions scripts/subscribe_webhook.ts
Original file line number Diff line number Diff line change
@@ -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);
10 changes: 10 additions & 0 deletions scripts/unsubscribe_webhook.ts
Original file line number Diff line number Diff line change
@@ -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);
10 changes: 10 additions & 0 deletions scripts/validate_bsb.ts
Original file line number Diff line number Diff line change
@@ -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);

0 comments on commit 91905bf

Please sign in to comment.