-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgermlin.ts
110 lines (91 loc) · 2.99 KB
/
germlin.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
import CryptoJS from 'crypto-js'
import { cache } from 'react-native-fetch-cache/cacheResolver'
export interface GermlinConfig {
masterkey: string;
serviceurl: string;
methodname: string;
}
export class Param {
constructor(name: string, value: string) {
this.name = name;
this.value = value
}
name: string;
value: string;
}
export interface FetchParam {
query: string;
parameters: Array<Param>,
actionName: string
auth?: string;
}
export class FetchParamDefualt implements FetchParam {
query: string;
parameters: Array<Param>;
actionName: string;
auth?: string;
constructor(query: string, parameters: Array<Param>, actionName: string, auth?: string) {
this.query = query;
this.parameters = parameters;
this.actionName = actionName;
this.auth = auth;
}
}
class AuthParam {
constructor(paramscount: number, methodname: string, date: string, key: string) {
this.paramscount = paramscount;
this.methodname = methodname;
this.date = date;
this.key = key
}
paramscount: number;
methodname: string;
date: string;
key: string
}
class AzureGermlin {
static myInstance: AzureGermlin = null;
static config: GermlinConfig;
static getInstance() {
if (AzureGermlin.myInstance == null) {
AzureGermlin.myInstance = new AzureGermlin();
}
return this.myInstance;
}
public fetch(param: FetchParam): Promise<Response> {
const isValid = cache(param.actionName, undefined)
if (isValid) {
const response = new Response(null, { status: 304, statusText: "cache" })
return new Promise<Response>((resolve, reject) => {
resolve(response)
});
}
const today = new Date();
const UTCstring = today.toUTCString();
const auth = param.auth ? param.auth : this.auth(new AuthParam(param.parameters.length, AzureGermlin.config.methodname, UTCstring, AzureGermlin.config.masterkey))
return fetch(AzureGermlin.config.serviceurl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': auth,
'x-ms-date': UTCstring
},
body: JSON.stringify({ query: param.query, parameters: param.parameters })
});
}
public auth(param: AuthParam) {
const signatureRaw = `POST${param.paramscount}${param.methodname}${param.date}`;
const signatureBytes = CryptoJS.HmacSHA256(signatureRaw, CryptoJS.enc.Base64.parse(param.key));
const signatureEncoded = signatureBytes.toString(CryptoJS.enc.Base64);
return signatureEncoded;
}
constructor() {
}
}
export const azuregermlinfetch = async (param: FetchParam) => {
let instance = AzureGermlin.getInstance();
return instance.fetch(param)
}
export const initCosmosGermlin = (config: GermlinConfig) => {
AzureGermlin.config = config;
}