-
Notifications
You must be signed in to change notification settings - Fork 1
/
platformClient.ts
108 lines (90 loc) · 2.72 KB
/
platformClient.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import { GraphQLClient } from "graphql-request";
import { RequestDocument, Variables } from "graphql-request/dist/types";
import { getSdk, Sdk } from "./graph";
import { inspect } from "util";
// import { version } from './../package.json';
const version = "1.0.21";
const btoa = string => {
const buffer = Buffer.from(string.toString(), "binary");
return buffer.toString("base64");
};
export enum PlatformTarget {
Sandbox,
Live
}
interface SdkInterface extends Sdk {}
/**
* Use an Authentication scheme to perform operations on behalf of a client.
* see {@link https://developers.joinblvd.com/2020-01/client-api/authentication} for details
* of how to generate a signed client token
*/
export type Authentication = {
token: string;
};
class Node<T> {
/**
* @internal
*/
protected platformClient: PlatformClient;
/**
* @internal
*/
constructor(platformClient: PlatformClient, graphItem: T) {
this.platformClient = platformClient;
Object.assign(this, graphItem);
}
}
class PlatformClient {
private authentication?: Authentication;
private client: GraphQLClient;
private url: string;
constructor(
private apiKey: string,
businessID: string,
public target?: PlatformTarget
) {
this.url = this.buildUrl(businessID, target);
this.client = this.buildClient();
}
private buildUrl(businessID: String, target?: PlatformTarget) {
switch (target) {
case PlatformTarget.Sandbox:
return `https://sandbox.joinblvd.com/api/2020-01/${businessID}/client`;
case PlatformTarget.Live:
return `https://dashboard.boulevard.io/api/2020-01/${businessID}/client`;
case undefined:
return `https://sandbox.joinblvd.com/api/2020-01/${businessID}/client`;
default:
return `${target}/api/2020-01/${businessID}/client`;
}
}
request(query: RequestDocument, variables?: Variables) {
const result = this.client.request(query, variables, this.headers());
return result;
}
withAuthentication(auth: Authentication): PlatformClient {
this.authentication = auth;
return this;
}
sdk(): SdkInterface {
return getSdk(this.buildClient());
}
authenticatedSdk(auth: Authentication): SdkInterface {
return this.withAuthentication(auth).sdk();
}
private buildClient(): GraphQLClient {
return new GraphQLClient(this.url, { headers: this.headers() });
}
private token() {
return this.authentication?.token
? btoa(`${this.apiKey}:${this.authentication.token}`)
: btoa(`${this.apiKey}:`);
}
private headers(): Record<"Authorization" | "Book-SDK-Version", string> {
return {
Authorization: `Basic ${this.token()}`,
"Book-SDK-Version": version
};
}
}
export { PlatformClient, Node };