-
Notifications
You must be signed in to change notification settings - Fork 43
/
shared.ts
90 lines (77 loc) · 2.9 KB
/
shared.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import { GiftCard } from "../../shared/types";
import { AccessToken, ReloadlyFailureResponse } from "./types";
export const commonHeaders = {
"Content-Type": "application/json",
Accept: "application/com.reloadly.giftcards-v1+json",
};
export interface Env {
USE_RELOADLY_SANDBOX: string;
RELOADLY_API_CLIENT_ID: string;
RELOADLY_API_CLIENT_SECRET: string;
}
export interface ReloadlyAuthResponse {
access_token: string;
scope: string;
expires_in: number;
token_type: string;
}
export const RELOADLY_AUTH_URL = "https://auth.reloadly.com/oauth/token";
export const RELOADLY_SANDBOX_API_URL = "https://giftcards-sandbox.reloadly.com";
export const RELOADLY_PRODUCTION_API_URL = "https://web3-gateway-test.com/proxy/reloadly/production";
export function getReloadlyApiBaseUrl(isSandbox: boolean): string {
if (isSandbox === false) {
return RELOADLY_PRODUCTION_API_URL;
}
return RELOADLY_SANDBOX_API_URL;
}
export async function getAccessToken(env: Env): Promise<AccessToken> {
console.log("Using Reloadly Sandbox:", env.USE_RELOADLY_SANDBOX !== "false");
const options = {
method: "POST",
headers: { "Content-Type": "application/json", Accept: "application/json" },
body: JSON.stringify({
client_id: env.RELOADLY_API_CLIENT_ID,
client_secret: env.RELOADLY_API_CLIENT_SECRET,
grant_type: "client_credentials",
audience: env.USE_RELOADLY_SANDBOX === "false" ? "https://giftcards.reloadly.com" : "https://giftcards-sandbox.reloadly.com",
}),
};
const res = await fetch(RELOADLY_AUTH_URL, options);
if (res.status == 200) {
const successResponse = (await res.json()) as ReloadlyAuthResponse;
return {
token: successResponse.access_token,
isSandbox: env.USE_RELOADLY_SANDBOX !== "false",
};
}
throw `Getting access token failed: ${JSON.stringify(await res.json())}`;
}
export async function getGiftCards(productQuery: string, country: string, accessToken: AccessToken): Promise<GiftCard[]> {
// productCategoryId = 1 = Finance.
// This should prevent mixing of other gift cards with similar keywords
const url = `${getReloadlyApiBaseUrl(accessToken.isSandbox)}/countries/${country}/products?productName=${productQuery}&productCategoryId=1`;
console.log(`Retrieving gift cards from ${url}`);
const options = {
method: "GET",
headers: {
...commonHeaders,
Authorization: `Bearer ${accessToken.token}`,
},
};
const response = await fetch(url, options);
const responseJson = await response.json();
console.log("Response status", response.status);
console.log(`Response from ${url}`, responseJson);
if (response.status == 404) {
return [];
}
if (response.status != 200) {
throw new Error(
`Error from Reloadly API: ${JSON.stringify({
status: response.status,
message: (responseJson as ReloadlyFailureResponse).message,
})}`
);
}
return responseJson as GiftCard[];
}