@nhost/nhost-js 0.3.5 → 0.3.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +4 -5
- package/dist/{cjs/clients → clients}/functions.d.ts +3 -3
- package/dist/{esm/clients → clients}/graphql.d.ts +3 -3
- package/dist/{cjs/core → core}/index.d.ts +0 -0
- package/dist/{esm/core → core}/nhost-client.d.ts +3 -12
- package/dist/index.cjs.js +1 -0
- package/dist/{cjs/index.d.ts → index.d.ts} +1 -2
- package/dist/index.es.js +1293 -0
- package/dist/{cjs/types.d.ts → types.d.ts} +6 -6
- package/package.json +39 -33
- package/src/index.ts +6 -0
- package/dist/cjs/clients/functions.js +0 -113
- package/dist/cjs/clients/functions.js.map +0 -1
- package/dist/cjs/clients/graphql.d.ts +0 -15
- package/dist/cjs/clients/graphql.js +0 -133
- package/dist/cjs/clients/graphql.js.map +0 -1
- package/dist/cjs/core/index.js +0 -14
- package/dist/cjs/core/index.js.map +0 -1
- package/dist/cjs/core/nhost-client.d.ts +0 -28
- package/dist/cjs/core/nhost-client.js +0 -59
- package/dist/cjs/core/nhost-client.js.map +0 -1
- package/dist/cjs/index.js +0 -20
- package/dist/cjs/index.js.map +0 -1
- package/dist/cjs/types.js +0 -3
- package/dist/cjs/types.js.map +0 -1
- package/dist/esm/clients/functions.d.ts +0 -13
- package/dist/esm/clients/functions.js +0 -107
- package/dist/esm/clients/functions.js.map +0 -1
- package/dist/esm/clients/graphql.js +0 -127
- package/dist/esm/clients/graphql.js.map +0 -1
- package/dist/esm/core/index.d.ts +0 -1
- package/dist/esm/core/index.js +0 -2
- package/dist/esm/core/index.js.map +0 -1
- package/dist/esm/core/nhost-client.js +0 -56
- package/dist/esm/core/nhost-client.js.map +0 -1
- package/dist/esm/index.d.ts +0 -5
- package/dist/esm/index.js +0 -7
- package/dist/esm/index.js.map +0 -1
- package/dist/esm/types.d.ts +0 -19
- package/dist/esm/types.js +0 -2
- package/dist/esm/types.js.map +0 -1
package/dist/index.es.js
ADDED
|
@@ -0,0 +1,1293 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defProps = Object.defineProperties;
|
|
3
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
4
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
7
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
+
var __spreadValues = (a, b) => {
|
|
9
|
+
for (var prop in b || (b = {}))
|
|
10
|
+
if (__hasOwnProp.call(b, prop))
|
|
11
|
+
__defNormalProp(a, prop, b[prop]);
|
|
12
|
+
if (__getOwnPropSymbols)
|
|
13
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
14
|
+
if (__propIsEnum.call(b, prop))
|
|
15
|
+
__defNormalProp(a, prop, b[prop]);
|
|
16
|
+
}
|
|
17
|
+
return a;
|
|
18
|
+
};
|
|
19
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
20
|
+
const axios$3 = require("axios");
|
|
21
|
+
const SERVER_ERROR_CODE = 500;
|
|
22
|
+
class HasuraAuthApi {
|
|
23
|
+
constructor({ url = "" }) {
|
|
24
|
+
this.url = url;
|
|
25
|
+
this.httpClient = axios$3.create({
|
|
26
|
+
baseURL: this.url
|
|
27
|
+
});
|
|
28
|
+
this.httpClient.interceptors.response.use((response) => response, (error) => {
|
|
29
|
+
var _a, _b, _c, _d, _e, _f;
|
|
30
|
+
return Promise.reject({
|
|
31
|
+
message: (_d = (_c = (_b = (_a = error.response) == null ? void 0 : _a.data) == null ? void 0 : _b.message) != null ? _c : error.message) != null ? _d : JSON.stringify(error),
|
|
32
|
+
status: (_f = (_e = error.response) == null ? void 0 : _e.status) != null ? _f : SERVER_ERROR_CODE
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
async signUpEmailPassword(params) {
|
|
37
|
+
try {
|
|
38
|
+
const res = await this.httpClient.post("/signup/email-password", params);
|
|
39
|
+
return { data: res.data, error: null };
|
|
40
|
+
} catch (error) {
|
|
41
|
+
return { data: null, error };
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
async signInEmailPassword(params) {
|
|
45
|
+
try {
|
|
46
|
+
const res = await this.httpClient.post("/signin/email-password", params);
|
|
47
|
+
return { data: res.data, error: null };
|
|
48
|
+
} catch (error) {
|
|
49
|
+
return { data: null, error };
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
async signInPasswordlessEmail(params) {
|
|
53
|
+
try {
|
|
54
|
+
const res = await this.httpClient.post("/signin/passwordless/email", params);
|
|
55
|
+
return { data: res.data, error: null };
|
|
56
|
+
} catch (error) {
|
|
57
|
+
return { data: null, error };
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
async signInPasswordlessSms(params) {
|
|
61
|
+
try {
|
|
62
|
+
const res = await this.httpClient.post("/signin/passwordless/sms", params);
|
|
63
|
+
return { data: res.data, error: null };
|
|
64
|
+
} catch (error) {
|
|
65
|
+
return { data: null, error };
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
async signInPasswordlessSmsOtp(params) {
|
|
69
|
+
try {
|
|
70
|
+
const res = await this.httpClient.post("/signin/passwordless/sms/otp", params);
|
|
71
|
+
return { data: res.data, error: null };
|
|
72
|
+
} catch (error) {
|
|
73
|
+
return { data: null, error };
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
async signOut(params) {
|
|
77
|
+
try {
|
|
78
|
+
await this.httpClient.post("/signout", params);
|
|
79
|
+
return { error: null };
|
|
80
|
+
} catch (error) {
|
|
81
|
+
return { error };
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
async refreshToken(params) {
|
|
85
|
+
try {
|
|
86
|
+
const res = await this.httpClient.post("/token", params);
|
|
87
|
+
return { error: null, session: res.data };
|
|
88
|
+
} catch (error) {
|
|
89
|
+
return { error, session: null };
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
async resetPassword(params) {
|
|
93
|
+
try {
|
|
94
|
+
await this.httpClient.post("/user/password/reset", params);
|
|
95
|
+
return { error: null };
|
|
96
|
+
} catch (error) {
|
|
97
|
+
return { error };
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
async changePassword(params) {
|
|
101
|
+
try {
|
|
102
|
+
await this.httpClient.post("/user/password", params, {
|
|
103
|
+
headers: __spreadValues({}, this.generateAuthHeaders())
|
|
104
|
+
});
|
|
105
|
+
return { error: null };
|
|
106
|
+
} catch (error) {
|
|
107
|
+
return { error };
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
async sendVerificationEmail(params) {
|
|
111
|
+
try {
|
|
112
|
+
await this.httpClient.post("/user/email/send-verification-email", params);
|
|
113
|
+
return { error: null };
|
|
114
|
+
} catch (error) {
|
|
115
|
+
return { error };
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
async changeEmail(params) {
|
|
119
|
+
try {
|
|
120
|
+
await this.httpClient.post("/user/email/change", params, {
|
|
121
|
+
headers: __spreadValues({}, this.generateAuthHeaders())
|
|
122
|
+
});
|
|
123
|
+
return { error: null };
|
|
124
|
+
} catch (error) {
|
|
125
|
+
return { error };
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
async deanonymize(params) {
|
|
129
|
+
try {
|
|
130
|
+
await this.httpClient.post("/user/deanonymize", params);
|
|
131
|
+
return { error: null };
|
|
132
|
+
} catch (error) {
|
|
133
|
+
return { error };
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
async verifyEmail(params) {
|
|
137
|
+
try {
|
|
138
|
+
const res = await this.httpClient.post("/user/email/verify", params);
|
|
139
|
+
return { data: res.data, error: null };
|
|
140
|
+
} catch (error) {
|
|
141
|
+
return { data: null, error };
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
setAccessToken(accessToken) {
|
|
145
|
+
this.accessToken = accessToken;
|
|
146
|
+
}
|
|
147
|
+
generateAuthHeaders() {
|
|
148
|
+
if (!this.accessToken) {
|
|
149
|
+
return null;
|
|
150
|
+
}
|
|
151
|
+
return {
|
|
152
|
+
Authorization: `Bearer ${this.accessToken}`
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
var queryString = {};
|
|
157
|
+
var strictUriEncode = (str) => encodeURIComponent(str).replace(/[!'()*]/g, (x) => `%${x.charCodeAt(0).toString(16).toUpperCase()}`);
|
|
158
|
+
var token = "%[a-f0-9]{2}";
|
|
159
|
+
var singleMatcher = new RegExp(token, "gi");
|
|
160
|
+
var multiMatcher = new RegExp("(" + token + ")+", "gi");
|
|
161
|
+
function decodeComponents(components, split) {
|
|
162
|
+
try {
|
|
163
|
+
return decodeURIComponent(components.join(""));
|
|
164
|
+
} catch (err) {
|
|
165
|
+
}
|
|
166
|
+
if (components.length === 1) {
|
|
167
|
+
return components;
|
|
168
|
+
}
|
|
169
|
+
split = split || 1;
|
|
170
|
+
var left = components.slice(0, split);
|
|
171
|
+
var right = components.slice(split);
|
|
172
|
+
return Array.prototype.concat.call([], decodeComponents(left), decodeComponents(right));
|
|
173
|
+
}
|
|
174
|
+
function decode(input) {
|
|
175
|
+
try {
|
|
176
|
+
return decodeURIComponent(input);
|
|
177
|
+
} catch (err) {
|
|
178
|
+
var tokens = input.match(singleMatcher);
|
|
179
|
+
for (var i = 1; i < tokens.length; i++) {
|
|
180
|
+
input = decodeComponents(tokens, i).join("");
|
|
181
|
+
tokens = input.match(singleMatcher);
|
|
182
|
+
}
|
|
183
|
+
return input;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
function customDecodeURIComponent(input) {
|
|
187
|
+
var replaceMap = {
|
|
188
|
+
"%FE%FF": "\uFFFD\uFFFD",
|
|
189
|
+
"%FF%FE": "\uFFFD\uFFFD"
|
|
190
|
+
};
|
|
191
|
+
var match = multiMatcher.exec(input);
|
|
192
|
+
while (match) {
|
|
193
|
+
try {
|
|
194
|
+
replaceMap[match[0]] = decodeURIComponent(match[0]);
|
|
195
|
+
} catch (err) {
|
|
196
|
+
var result = decode(match[0]);
|
|
197
|
+
if (result !== match[0]) {
|
|
198
|
+
replaceMap[match[0]] = result;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
match = multiMatcher.exec(input);
|
|
202
|
+
}
|
|
203
|
+
replaceMap["%C2"] = "\uFFFD";
|
|
204
|
+
var entries = Object.keys(replaceMap);
|
|
205
|
+
for (var i = 0; i < entries.length; i++) {
|
|
206
|
+
var key = entries[i];
|
|
207
|
+
input = input.replace(new RegExp(key, "g"), replaceMap[key]);
|
|
208
|
+
}
|
|
209
|
+
return input;
|
|
210
|
+
}
|
|
211
|
+
var decodeUriComponent = function(encodedURI) {
|
|
212
|
+
if (typeof encodedURI !== "string") {
|
|
213
|
+
throw new TypeError("Expected `encodedURI` to be of type `string`, got `" + typeof encodedURI + "`");
|
|
214
|
+
}
|
|
215
|
+
try {
|
|
216
|
+
encodedURI = encodedURI.replace(/\+/g, " ");
|
|
217
|
+
return decodeURIComponent(encodedURI);
|
|
218
|
+
} catch (err) {
|
|
219
|
+
return customDecodeURIComponent(encodedURI);
|
|
220
|
+
}
|
|
221
|
+
};
|
|
222
|
+
var splitOnFirst = (string, separator) => {
|
|
223
|
+
if (!(typeof string === "string" && typeof separator === "string")) {
|
|
224
|
+
throw new TypeError("Expected the arguments to be of type `string`");
|
|
225
|
+
}
|
|
226
|
+
if (separator === "") {
|
|
227
|
+
return [string];
|
|
228
|
+
}
|
|
229
|
+
const separatorIndex = string.indexOf(separator);
|
|
230
|
+
if (separatorIndex === -1) {
|
|
231
|
+
return [string];
|
|
232
|
+
}
|
|
233
|
+
return [
|
|
234
|
+
string.slice(0, separatorIndex),
|
|
235
|
+
string.slice(separatorIndex + separator.length)
|
|
236
|
+
];
|
|
237
|
+
};
|
|
238
|
+
var filterObj = function(obj, predicate) {
|
|
239
|
+
var ret = {};
|
|
240
|
+
var keys = Object.keys(obj);
|
|
241
|
+
var isArr = Array.isArray(predicate);
|
|
242
|
+
for (var i = 0; i < keys.length; i++) {
|
|
243
|
+
var key = keys[i];
|
|
244
|
+
var val = obj[key];
|
|
245
|
+
if (isArr ? predicate.indexOf(key) !== -1 : predicate(key, val, obj)) {
|
|
246
|
+
ret[key] = val;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
return ret;
|
|
250
|
+
};
|
|
251
|
+
(function(exports) {
|
|
252
|
+
const strictUriEncode$1 = strictUriEncode;
|
|
253
|
+
const decodeComponent = decodeUriComponent;
|
|
254
|
+
const splitOnFirst$1 = splitOnFirst;
|
|
255
|
+
const filterObject = filterObj;
|
|
256
|
+
const isNullOrUndefined = (value) => value === null || value === void 0;
|
|
257
|
+
const encodeFragmentIdentifier = Symbol("encodeFragmentIdentifier");
|
|
258
|
+
function encoderForArrayFormat(options) {
|
|
259
|
+
switch (options.arrayFormat) {
|
|
260
|
+
case "index":
|
|
261
|
+
return (key) => (result, value) => {
|
|
262
|
+
const index = result.length;
|
|
263
|
+
if (value === void 0 || options.skipNull && value === null || options.skipEmptyString && value === "") {
|
|
264
|
+
return result;
|
|
265
|
+
}
|
|
266
|
+
if (value === null) {
|
|
267
|
+
return [...result, [encode(key, options), "[", index, "]"].join("")];
|
|
268
|
+
}
|
|
269
|
+
return [
|
|
270
|
+
...result,
|
|
271
|
+
[encode(key, options), "[", encode(index, options), "]=", encode(value, options)].join("")
|
|
272
|
+
];
|
|
273
|
+
};
|
|
274
|
+
case "bracket":
|
|
275
|
+
return (key) => (result, value) => {
|
|
276
|
+
if (value === void 0 || options.skipNull && value === null || options.skipEmptyString && value === "") {
|
|
277
|
+
return result;
|
|
278
|
+
}
|
|
279
|
+
if (value === null) {
|
|
280
|
+
return [...result, [encode(key, options), "[]"].join("")];
|
|
281
|
+
}
|
|
282
|
+
return [...result, [encode(key, options), "[]=", encode(value, options)].join("")];
|
|
283
|
+
};
|
|
284
|
+
case "colon-list-separator":
|
|
285
|
+
return (key) => (result, value) => {
|
|
286
|
+
if (value === void 0 || options.skipNull && value === null || options.skipEmptyString && value === "") {
|
|
287
|
+
return result;
|
|
288
|
+
}
|
|
289
|
+
if (value === null) {
|
|
290
|
+
return [...result, [encode(key, options), ":list="].join("")];
|
|
291
|
+
}
|
|
292
|
+
return [...result, [encode(key, options), ":list=", encode(value, options)].join("")];
|
|
293
|
+
};
|
|
294
|
+
case "comma":
|
|
295
|
+
case "separator":
|
|
296
|
+
case "bracket-separator": {
|
|
297
|
+
const keyValueSep = options.arrayFormat === "bracket-separator" ? "[]=" : "=";
|
|
298
|
+
return (key) => (result, value) => {
|
|
299
|
+
if (value === void 0 || options.skipNull && value === null || options.skipEmptyString && value === "") {
|
|
300
|
+
return result;
|
|
301
|
+
}
|
|
302
|
+
value = value === null ? "" : value;
|
|
303
|
+
if (result.length === 0) {
|
|
304
|
+
return [[encode(key, options), keyValueSep, encode(value, options)].join("")];
|
|
305
|
+
}
|
|
306
|
+
return [[result, encode(value, options)].join(options.arrayFormatSeparator)];
|
|
307
|
+
};
|
|
308
|
+
}
|
|
309
|
+
default:
|
|
310
|
+
return (key) => (result, value) => {
|
|
311
|
+
if (value === void 0 || options.skipNull && value === null || options.skipEmptyString && value === "") {
|
|
312
|
+
return result;
|
|
313
|
+
}
|
|
314
|
+
if (value === null) {
|
|
315
|
+
return [...result, encode(key, options)];
|
|
316
|
+
}
|
|
317
|
+
return [...result, [encode(key, options), "=", encode(value, options)].join("")];
|
|
318
|
+
};
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
function parserForArrayFormat(options) {
|
|
322
|
+
let result;
|
|
323
|
+
switch (options.arrayFormat) {
|
|
324
|
+
case "index":
|
|
325
|
+
return (key, value, accumulator) => {
|
|
326
|
+
result = /\[(\d*)\]$/.exec(key);
|
|
327
|
+
key = key.replace(/\[\d*\]$/, "");
|
|
328
|
+
if (!result) {
|
|
329
|
+
accumulator[key] = value;
|
|
330
|
+
return;
|
|
331
|
+
}
|
|
332
|
+
if (accumulator[key] === void 0) {
|
|
333
|
+
accumulator[key] = {};
|
|
334
|
+
}
|
|
335
|
+
accumulator[key][result[1]] = value;
|
|
336
|
+
};
|
|
337
|
+
case "bracket":
|
|
338
|
+
return (key, value, accumulator) => {
|
|
339
|
+
result = /(\[\])$/.exec(key);
|
|
340
|
+
key = key.replace(/\[\]$/, "");
|
|
341
|
+
if (!result) {
|
|
342
|
+
accumulator[key] = value;
|
|
343
|
+
return;
|
|
344
|
+
}
|
|
345
|
+
if (accumulator[key] === void 0) {
|
|
346
|
+
accumulator[key] = [value];
|
|
347
|
+
return;
|
|
348
|
+
}
|
|
349
|
+
accumulator[key] = [].concat(accumulator[key], value);
|
|
350
|
+
};
|
|
351
|
+
case "colon-list-separator":
|
|
352
|
+
return (key, value, accumulator) => {
|
|
353
|
+
result = /(:list)$/.exec(key);
|
|
354
|
+
key = key.replace(/:list$/, "");
|
|
355
|
+
if (!result) {
|
|
356
|
+
accumulator[key] = value;
|
|
357
|
+
return;
|
|
358
|
+
}
|
|
359
|
+
if (accumulator[key] === void 0) {
|
|
360
|
+
accumulator[key] = [value];
|
|
361
|
+
return;
|
|
362
|
+
}
|
|
363
|
+
accumulator[key] = [].concat(accumulator[key], value);
|
|
364
|
+
};
|
|
365
|
+
case "comma":
|
|
366
|
+
case "separator":
|
|
367
|
+
return (key, value, accumulator) => {
|
|
368
|
+
const isArray = typeof value === "string" && value.includes(options.arrayFormatSeparator);
|
|
369
|
+
const isEncodedArray = typeof value === "string" && !isArray && decode2(value, options).includes(options.arrayFormatSeparator);
|
|
370
|
+
value = isEncodedArray ? decode2(value, options) : value;
|
|
371
|
+
const newValue = isArray || isEncodedArray ? value.split(options.arrayFormatSeparator).map((item) => decode2(item, options)) : value === null ? value : decode2(value, options);
|
|
372
|
+
accumulator[key] = newValue;
|
|
373
|
+
};
|
|
374
|
+
case "bracket-separator":
|
|
375
|
+
return (key, value, accumulator) => {
|
|
376
|
+
const isArray = /(\[\])$/.test(key);
|
|
377
|
+
key = key.replace(/\[\]$/, "");
|
|
378
|
+
if (!isArray) {
|
|
379
|
+
accumulator[key] = value ? decode2(value, options) : value;
|
|
380
|
+
return;
|
|
381
|
+
}
|
|
382
|
+
const arrayValue = value === null ? [] : value.split(options.arrayFormatSeparator).map((item) => decode2(item, options));
|
|
383
|
+
if (accumulator[key] === void 0) {
|
|
384
|
+
accumulator[key] = arrayValue;
|
|
385
|
+
return;
|
|
386
|
+
}
|
|
387
|
+
accumulator[key] = [].concat(accumulator[key], arrayValue);
|
|
388
|
+
};
|
|
389
|
+
default:
|
|
390
|
+
return (key, value, accumulator) => {
|
|
391
|
+
if (accumulator[key] === void 0) {
|
|
392
|
+
accumulator[key] = value;
|
|
393
|
+
return;
|
|
394
|
+
}
|
|
395
|
+
accumulator[key] = [].concat(accumulator[key], value);
|
|
396
|
+
};
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
function validateArrayFormatSeparator(value) {
|
|
400
|
+
if (typeof value !== "string" || value.length !== 1) {
|
|
401
|
+
throw new TypeError("arrayFormatSeparator must be single character string");
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
function encode(value, options) {
|
|
405
|
+
if (options.encode) {
|
|
406
|
+
return options.strict ? strictUriEncode$1(value) : encodeURIComponent(value);
|
|
407
|
+
}
|
|
408
|
+
return value;
|
|
409
|
+
}
|
|
410
|
+
function decode2(value, options) {
|
|
411
|
+
if (options.decode) {
|
|
412
|
+
return decodeComponent(value);
|
|
413
|
+
}
|
|
414
|
+
return value;
|
|
415
|
+
}
|
|
416
|
+
function keysSorter(input) {
|
|
417
|
+
if (Array.isArray(input)) {
|
|
418
|
+
return input.sort();
|
|
419
|
+
}
|
|
420
|
+
if (typeof input === "object") {
|
|
421
|
+
return keysSorter(Object.keys(input)).sort((a, b) => Number(a) - Number(b)).map((key) => input[key]);
|
|
422
|
+
}
|
|
423
|
+
return input;
|
|
424
|
+
}
|
|
425
|
+
function removeHash(input) {
|
|
426
|
+
const hashStart = input.indexOf("#");
|
|
427
|
+
if (hashStart !== -1) {
|
|
428
|
+
input = input.slice(0, hashStart);
|
|
429
|
+
}
|
|
430
|
+
return input;
|
|
431
|
+
}
|
|
432
|
+
function getHash(url) {
|
|
433
|
+
let hash = "";
|
|
434
|
+
const hashStart = url.indexOf("#");
|
|
435
|
+
if (hashStart !== -1) {
|
|
436
|
+
hash = url.slice(hashStart);
|
|
437
|
+
}
|
|
438
|
+
return hash;
|
|
439
|
+
}
|
|
440
|
+
function extract(input) {
|
|
441
|
+
input = removeHash(input);
|
|
442
|
+
const queryStart = input.indexOf("?");
|
|
443
|
+
if (queryStart === -1) {
|
|
444
|
+
return "";
|
|
445
|
+
}
|
|
446
|
+
return input.slice(queryStart + 1);
|
|
447
|
+
}
|
|
448
|
+
function parseValue(value, options) {
|
|
449
|
+
if (options.parseNumbers && !Number.isNaN(Number(value)) && (typeof value === "string" && value.trim() !== "")) {
|
|
450
|
+
value = Number(value);
|
|
451
|
+
} else if (options.parseBooleans && value !== null && (value.toLowerCase() === "true" || value.toLowerCase() === "false")) {
|
|
452
|
+
value = value.toLowerCase() === "true";
|
|
453
|
+
}
|
|
454
|
+
return value;
|
|
455
|
+
}
|
|
456
|
+
function parse(query, options) {
|
|
457
|
+
options = Object.assign({
|
|
458
|
+
decode: true,
|
|
459
|
+
sort: true,
|
|
460
|
+
arrayFormat: "none",
|
|
461
|
+
arrayFormatSeparator: ",",
|
|
462
|
+
parseNumbers: false,
|
|
463
|
+
parseBooleans: false
|
|
464
|
+
}, options);
|
|
465
|
+
validateArrayFormatSeparator(options.arrayFormatSeparator);
|
|
466
|
+
const formatter = parserForArrayFormat(options);
|
|
467
|
+
const ret = Object.create(null);
|
|
468
|
+
if (typeof query !== "string") {
|
|
469
|
+
return ret;
|
|
470
|
+
}
|
|
471
|
+
query = query.trim().replace(/^[?#&]/, "");
|
|
472
|
+
if (!query) {
|
|
473
|
+
return ret;
|
|
474
|
+
}
|
|
475
|
+
for (const param of query.split("&")) {
|
|
476
|
+
if (param === "") {
|
|
477
|
+
continue;
|
|
478
|
+
}
|
|
479
|
+
let [key, value] = splitOnFirst$1(options.decode ? param.replace(/\+/g, " ") : param, "=");
|
|
480
|
+
value = value === void 0 ? null : ["comma", "separator", "bracket-separator"].includes(options.arrayFormat) ? value : decode2(value, options);
|
|
481
|
+
formatter(decode2(key, options), value, ret);
|
|
482
|
+
}
|
|
483
|
+
for (const key of Object.keys(ret)) {
|
|
484
|
+
const value = ret[key];
|
|
485
|
+
if (typeof value === "object" && value !== null) {
|
|
486
|
+
for (const k of Object.keys(value)) {
|
|
487
|
+
value[k] = parseValue(value[k], options);
|
|
488
|
+
}
|
|
489
|
+
} else {
|
|
490
|
+
ret[key] = parseValue(value, options);
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
if (options.sort === false) {
|
|
494
|
+
return ret;
|
|
495
|
+
}
|
|
496
|
+
return (options.sort === true ? Object.keys(ret).sort() : Object.keys(ret).sort(options.sort)).reduce((result, key) => {
|
|
497
|
+
const value = ret[key];
|
|
498
|
+
if (Boolean(value) && typeof value === "object" && !Array.isArray(value)) {
|
|
499
|
+
result[key] = keysSorter(value);
|
|
500
|
+
} else {
|
|
501
|
+
result[key] = value;
|
|
502
|
+
}
|
|
503
|
+
return result;
|
|
504
|
+
}, Object.create(null));
|
|
505
|
+
}
|
|
506
|
+
exports.extract = extract;
|
|
507
|
+
exports.parse = parse;
|
|
508
|
+
exports.stringify = (object, options) => {
|
|
509
|
+
if (!object) {
|
|
510
|
+
return "";
|
|
511
|
+
}
|
|
512
|
+
options = Object.assign({
|
|
513
|
+
encode: true,
|
|
514
|
+
strict: true,
|
|
515
|
+
arrayFormat: "none",
|
|
516
|
+
arrayFormatSeparator: ","
|
|
517
|
+
}, options);
|
|
518
|
+
validateArrayFormatSeparator(options.arrayFormatSeparator);
|
|
519
|
+
const shouldFilter = (key) => options.skipNull && isNullOrUndefined(object[key]) || options.skipEmptyString && object[key] === "";
|
|
520
|
+
const formatter = encoderForArrayFormat(options);
|
|
521
|
+
const objectCopy = {};
|
|
522
|
+
for (const key of Object.keys(object)) {
|
|
523
|
+
if (!shouldFilter(key)) {
|
|
524
|
+
objectCopy[key] = object[key];
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
const keys = Object.keys(objectCopy);
|
|
528
|
+
if (options.sort !== false) {
|
|
529
|
+
keys.sort(options.sort);
|
|
530
|
+
}
|
|
531
|
+
return keys.map((key) => {
|
|
532
|
+
const value = object[key];
|
|
533
|
+
if (value === void 0) {
|
|
534
|
+
return "";
|
|
535
|
+
}
|
|
536
|
+
if (value === null) {
|
|
537
|
+
return encode(key, options);
|
|
538
|
+
}
|
|
539
|
+
if (Array.isArray(value)) {
|
|
540
|
+
if (value.length === 0 && options.arrayFormat === "bracket-separator") {
|
|
541
|
+
return encode(key, options) + "[]";
|
|
542
|
+
}
|
|
543
|
+
return value.reduce(formatter(key), []).join("&");
|
|
544
|
+
}
|
|
545
|
+
return encode(key, options) + "=" + encode(value, options);
|
|
546
|
+
}).filter((x) => x.length > 0).join("&");
|
|
547
|
+
};
|
|
548
|
+
exports.parseUrl = (url, options) => {
|
|
549
|
+
options = Object.assign({
|
|
550
|
+
decode: true
|
|
551
|
+
}, options);
|
|
552
|
+
const [url_, hash] = splitOnFirst$1(url, "#");
|
|
553
|
+
return Object.assign({
|
|
554
|
+
url: url_.split("?")[0] || "",
|
|
555
|
+
query: parse(extract(url), options)
|
|
556
|
+
}, options && options.parseFragmentIdentifier && hash ? { fragmentIdentifier: decode2(hash, options) } : {});
|
|
557
|
+
};
|
|
558
|
+
exports.stringifyUrl = (object, options) => {
|
|
559
|
+
options = Object.assign({
|
|
560
|
+
encode: true,
|
|
561
|
+
strict: true,
|
|
562
|
+
[encodeFragmentIdentifier]: true
|
|
563
|
+
}, options);
|
|
564
|
+
const url = removeHash(object.url).split("?")[0] || "";
|
|
565
|
+
const queryFromUrl = exports.extract(object.url);
|
|
566
|
+
const parsedQueryFromUrl = exports.parse(queryFromUrl, { sort: false });
|
|
567
|
+
const query = Object.assign(parsedQueryFromUrl, object.query);
|
|
568
|
+
let queryString2 = exports.stringify(query, options);
|
|
569
|
+
if (queryString2) {
|
|
570
|
+
queryString2 = `?${queryString2}`;
|
|
571
|
+
}
|
|
572
|
+
let hash = getHash(object.url);
|
|
573
|
+
if (object.fragmentIdentifier) {
|
|
574
|
+
hash = `#${options[encodeFragmentIdentifier] ? encode(object.fragmentIdentifier, options) : object.fragmentIdentifier}`;
|
|
575
|
+
}
|
|
576
|
+
return `${url}${queryString2}${hash}`;
|
|
577
|
+
};
|
|
578
|
+
exports.pick = (input, filter, options) => {
|
|
579
|
+
options = Object.assign({
|
|
580
|
+
parseFragmentIdentifier: true,
|
|
581
|
+
[encodeFragmentIdentifier]: false
|
|
582
|
+
}, options);
|
|
583
|
+
const { url, query, fragmentIdentifier } = exports.parseUrl(input, options);
|
|
584
|
+
return exports.stringifyUrl({
|
|
585
|
+
url,
|
|
586
|
+
query: filterObject(query, filter),
|
|
587
|
+
fragmentIdentifier
|
|
588
|
+
}, options);
|
|
589
|
+
};
|
|
590
|
+
exports.exclude = (input, filter, options) => {
|
|
591
|
+
const exclusionFilter = Array.isArray(filter) ? (key) => !filter.includes(key) : (key, value) => !filter(key, value);
|
|
592
|
+
return exports.pick(input, exclusionFilter, options);
|
|
593
|
+
};
|
|
594
|
+
})(queryString);
|
|
595
|
+
const NHOST_REFRESH_TOKEN = "nhostRefreshToken";
|
|
596
|
+
const isBrowser = () => typeof window !== "undefined";
|
|
597
|
+
class inMemoryLocalStorage {
|
|
598
|
+
constructor() {
|
|
599
|
+
this.memory = {};
|
|
600
|
+
}
|
|
601
|
+
setItem(key, value) {
|
|
602
|
+
this.memory[key] = value;
|
|
603
|
+
}
|
|
604
|
+
getItem(key) {
|
|
605
|
+
return this.memory[key];
|
|
606
|
+
}
|
|
607
|
+
removeItem(key) {
|
|
608
|
+
delete this.memory[key];
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
class HasuraAuthClient {
|
|
612
|
+
constructor({
|
|
613
|
+
url,
|
|
614
|
+
autoRefreshToken = true,
|
|
615
|
+
autoLogin = true,
|
|
616
|
+
refreshIntervalTime,
|
|
617
|
+
clientStorage,
|
|
618
|
+
clientStorageType = "web"
|
|
619
|
+
}) {
|
|
620
|
+
this.refreshIntervalTime = refreshIntervalTime;
|
|
621
|
+
if (!clientStorage) {
|
|
622
|
+
this.clientStorage = isBrowser() ? localStorage : new inMemoryLocalStorage();
|
|
623
|
+
} else {
|
|
624
|
+
this.clientStorage = clientStorage;
|
|
625
|
+
}
|
|
626
|
+
this.clientStorageType = clientStorageType;
|
|
627
|
+
this.onTokenChangedFunctions = [];
|
|
628
|
+
this.onAuthChangedFunctions = [];
|
|
629
|
+
this.refreshInterval;
|
|
630
|
+
this.refreshSleepCheckInterval = 0;
|
|
631
|
+
this.refreshIntervalSleepCheckLastSample = Date.now();
|
|
632
|
+
this.sampleRate = 2e3;
|
|
633
|
+
this.url = url;
|
|
634
|
+
this.autoRefreshToken = autoRefreshToken;
|
|
635
|
+
this.initAuthLoading = true;
|
|
636
|
+
this.session = null;
|
|
637
|
+
this.api = new HasuraAuthApi({ url: this.url });
|
|
638
|
+
let refreshToken = null;
|
|
639
|
+
let autoLoginFromQueryParameters = false;
|
|
640
|
+
if (autoLogin && isBrowser() && window.location) {
|
|
641
|
+
const urlParams = queryString.parse(window.location.toString().split("#")[1]);
|
|
642
|
+
if ("refreshToken" in urlParams) {
|
|
643
|
+
refreshToken = urlParams.refreshToken;
|
|
644
|
+
}
|
|
645
|
+
if ("otp" in urlParams && "email" in urlParams) {
|
|
646
|
+
const { otp, email } = urlParams;
|
|
647
|
+
this.signIn({
|
|
648
|
+
otp,
|
|
649
|
+
email
|
|
650
|
+
});
|
|
651
|
+
autoLoginFromQueryParameters = true;
|
|
652
|
+
}
|
|
653
|
+
}
|
|
654
|
+
if (!autoLoginFromQueryParameters && autoLogin) {
|
|
655
|
+
this._autoLogin(refreshToken);
|
|
656
|
+
} else if (refreshToken) {
|
|
657
|
+
this._setItem(NHOST_REFRESH_TOKEN, refreshToken);
|
|
658
|
+
}
|
|
659
|
+
}
|
|
660
|
+
async signUp(params) {
|
|
661
|
+
const { email, password } = params;
|
|
662
|
+
if (email && password) {
|
|
663
|
+
const { data, error } = await this.api.signUpEmailPassword(params);
|
|
664
|
+
if (error) {
|
|
665
|
+
return { session: null, error };
|
|
666
|
+
}
|
|
667
|
+
if (!data) {
|
|
668
|
+
return {
|
|
669
|
+
session: null,
|
|
670
|
+
error: { message: "An error occurred on sign up.", status: 500 }
|
|
671
|
+
};
|
|
672
|
+
}
|
|
673
|
+
const { session } = data;
|
|
674
|
+
if (session) {
|
|
675
|
+
await this._setSession(session);
|
|
676
|
+
}
|
|
677
|
+
return { session, error: null };
|
|
678
|
+
}
|
|
679
|
+
return {
|
|
680
|
+
session: null,
|
|
681
|
+
error: { message: "Incorrect parameters", status: 500 }
|
|
682
|
+
};
|
|
683
|
+
}
|
|
684
|
+
async signIn(params) {
|
|
685
|
+
if ("provider" in params) {
|
|
686
|
+
const { provider } = params;
|
|
687
|
+
const providerUrl = `${this.url}/signin/provider/${provider}`;
|
|
688
|
+
if (isBrowser()) {
|
|
689
|
+
window.location.href = providerUrl;
|
|
690
|
+
}
|
|
691
|
+
return { providerUrl, provider, session: null, mfa: null, error: null };
|
|
692
|
+
}
|
|
693
|
+
if ("email" in params && "password" in params) {
|
|
694
|
+
const { data, error } = await this.api.signInEmailPassword(params);
|
|
695
|
+
if (error) {
|
|
696
|
+
return { session: null, mfa: null, error };
|
|
697
|
+
}
|
|
698
|
+
if (!data) {
|
|
699
|
+
return {
|
|
700
|
+
session: null,
|
|
701
|
+
mfa: null,
|
|
702
|
+
error: { message: "Incorrect Data", status: 500 }
|
|
703
|
+
};
|
|
704
|
+
}
|
|
705
|
+
const { session, mfa } = data;
|
|
706
|
+
if (session) {
|
|
707
|
+
await this._setSession(session);
|
|
708
|
+
}
|
|
709
|
+
return { session, mfa, error: null };
|
|
710
|
+
}
|
|
711
|
+
if ("email" in params && !("otp" in params)) {
|
|
712
|
+
const { error } = await this.api.signInPasswordlessEmail(params);
|
|
713
|
+
if (error) {
|
|
714
|
+
return { session: null, mfa: null, error };
|
|
715
|
+
}
|
|
716
|
+
return { session: null, mfa: null, error: null };
|
|
717
|
+
}
|
|
718
|
+
if ("phoneNumber" in params && !("otp" in params)) {
|
|
719
|
+
const { error } = await this.api.signInPasswordlessSms(params);
|
|
720
|
+
if (error) {
|
|
721
|
+
return { session: null, mfa: null, error };
|
|
722
|
+
}
|
|
723
|
+
return { session: null, mfa: null, error: null };
|
|
724
|
+
}
|
|
725
|
+
if ("otp" in params) {
|
|
726
|
+
const { data, error } = await this.api.signInPasswordlessSmsOtp(params);
|
|
727
|
+
if (error) {
|
|
728
|
+
return { session: null, mfa: null, error };
|
|
729
|
+
}
|
|
730
|
+
if (!data) {
|
|
731
|
+
return {
|
|
732
|
+
session: null,
|
|
733
|
+
mfa: null,
|
|
734
|
+
error: { message: "Incorrect data", status: 500 }
|
|
735
|
+
};
|
|
736
|
+
}
|
|
737
|
+
const { session, mfa } = data;
|
|
738
|
+
if (session) {
|
|
739
|
+
await this._setSession(session);
|
|
740
|
+
}
|
|
741
|
+
return { session, mfa, error: null };
|
|
742
|
+
}
|
|
743
|
+
return {
|
|
744
|
+
session: null,
|
|
745
|
+
mfa: null,
|
|
746
|
+
error: { message: "Incorrect parameters", status: 500 }
|
|
747
|
+
};
|
|
748
|
+
}
|
|
749
|
+
async signOut(params) {
|
|
750
|
+
const refreshToken = await this._getItem(NHOST_REFRESH_TOKEN);
|
|
751
|
+
this._clearSession();
|
|
752
|
+
const { error } = await this.api.signOut({
|
|
753
|
+
refreshToken,
|
|
754
|
+
all: params == null ? void 0 : params.all
|
|
755
|
+
});
|
|
756
|
+
return { error };
|
|
757
|
+
}
|
|
758
|
+
async verifyEmail(params) {
|
|
759
|
+
return await this.api.verifyEmail(params);
|
|
760
|
+
}
|
|
761
|
+
async resetPassword(params) {
|
|
762
|
+
const { error } = await this.api.resetPassword(params);
|
|
763
|
+
return { error };
|
|
764
|
+
}
|
|
765
|
+
async changePassword(params) {
|
|
766
|
+
const { error } = await this.api.changePassword(params);
|
|
767
|
+
return { error };
|
|
768
|
+
}
|
|
769
|
+
async sendVerificationEmail(params) {
|
|
770
|
+
const { error } = await this.api.sendVerificationEmail(params);
|
|
771
|
+
return { error };
|
|
772
|
+
}
|
|
773
|
+
async changeEmail(params) {
|
|
774
|
+
const { error } = await this.api.changeEmail(params);
|
|
775
|
+
return { error };
|
|
776
|
+
}
|
|
777
|
+
async deanonymize(params) {
|
|
778
|
+
const { error } = await this.api.deanonymize(params);
|
|
779
|
+
return { error };
|
|
780
|
+
}
|
|
781
|
+
onTokenChanged(fn) {
|
|
782
|
+
this.onTokenChangedFunctions.push(fn);
|
|
783
|
+
const index = this.onTokenChangedFunctions.length - 1;
|
|
784
|
+
const unsubscribe = () => {
|
|
785
|
+
try {
|
|
786
|
+
this.onTokenChangedFunctions[index] = () => {
|
|
787
|
+
};
|
|
788
|
+
} catch {
|
|
789
|
+
console.warn("Unable to unsubscribe onTokenChanged function. Maybe the functions is already unsubscribed?");
|
|
790
|
+
}
|
|
791
|
+
};
|
|
792
|
+
return unsubscribe;
|
|
793
|
+
}
|
|
794
|
+
onAuthStateChanged(fn) {
|
|
795
|
+
this.onAuthChangedFunctions.push(fn);
|
|
796
|
+
const index = this.onAuthChangedFunctions.length - 1;
|
|
797
|
+
const unsubscribe = () => {
|
|
798
|
+
try {
|
|
799
|
+
this.onAuthChangedFunctions[index] = () => {
|
|
800
|
+
};
|
|
801
|
+
} catch {
|
|
802
|
+
console.warn("Unable to unsubscribe onAuthStateChanged function. Maybe you already did?");
|
|
803
|
+
}
|
|
804
|
+
};
|
|
805
|
+
return unsubscribe;
|
|
806
|
+
}
|
|
807
|
+
isAuthenticated() {
|
|
808
|
+
return this.session !== null;
|
|
809
|
+
}
|
|
810
|
+
async isAuthenticatedAsync() {
|
|
811
|
+
return new Promise((resolve) => {
|
|
812
|
+
if (!this.initAuthLoading) {
|
|
813
|
+
resolve(this.isAuthenticated());
|
|
814
|
+
} else {
|
|
815
|
+
const unsubscribe = this.onAuthStateChanged((event, _session) => {
|
|
816
|
+
resolve(event === "SIGNED_IN");
|
|
817
|
+
unsubscribe();
|
|
818
|
+
});
|
|
819
|
+
}
|
|
820
|
+
});
|
|
821
|
+
}
|
|
822
|
+
getAuthenticationStatus() {
|
|
823
|
+
if (this.initAuthLoading)
|
|
824
|
+
return { isAuthenticated: false, isLoading: true };
|
|
825
|
+
return { isAuthenticated: this.session !== null, isLoading: false };
|
|
826
|
+
}
|
|
827
|
+
getJWTToken() {
|
|
828
|
+
return this.getAccessToken();
|
|
829
|
+
}
|
|
830
|
+
getAccessToken() {
|
|
831
|
+
if (!this.session) {
|
|
832
|
+
return void 0;
|
|
833
|
+
}
|
|
834
|
+
return this.session.accessToken;
|
|
835
|
+
}
|
|
836
|
+
async refreshSession(refreshToken) {
|
|
837
|
+
const refreshTokenToUse = refreshToken || await this._getItem(NHOST_REFRESH_TOKEN);
|
|
838
|
+
if (!refreshTokenToUse) {
|
|
839
|
+
console.warn("no refresh token found. No way of refreshing session");
|
|
840
|
+
}
|
|
841
|
+
return this._refreshTokens(refreshTokenToUse);
|
|
842
|
+
}
|
|
843
|
+
getSession() {
|
|
844
|
+
return this.session;
|
|
845
|
+
}
|
|
846
|
+
getUser() {
|
|
847
|
+
return this.session ? this.session.user : null;
|
|
848
|
+
}
|
|
849
|
+
async _setItem(key, value) {
|
|
850
|
+
if (typeof value !== "string") {
|
|
851
|
+
console.error(`value is not of type "string"`);
|
|
852
|
+
return;
|
|
853
|
+
}
|
|
854
|
+
switch (this.clientStorageType) {
|
|
855
|
+
case "web":
|
|
856
|
+
if (typeof this.clientStorage.setItem !== "function") {
|
|
857
|
+
console.error(`this.clientStorage.setItem is not a function`);
|
|
858
|
+
break;
|
|
859
|
+
}
|
|
860
|
+
this.clientStorage.setItem(key, value);
|
|
861
|
+
break;
|
|
862
|
+
case "custom":
|
|
863
|
+
case "react-native":
|
|
864
|
+
if (typeof this.clientStorage.setItem !== "function") {
|
|
865
|
+
console.error(`this.clientStorage.setItem is not a function`);
|
|
866
|
+
break;
|
|
867
|
+
}
|
|
868
|
+
await this.clientStorage.setItem(key, value);
|
|
869
|
+
break;
|
|
870
|
+
case "capacitor":
|
|
871
|
+
if (typeof this.clientStorage.set !== "function") {
|
|
872
|
+
console.error(`this.clientStorage.set is not a function`);
|
|
873
|
+
break;
|
|
874
|
+
}
|
|
875
|
+
await this.clientStorage.set({ key, value });
|
|
876
|
+
break;
|
|
877
|
+
case "expo-secure-storage":
|
|
878
|
+
if (typeof this.clientStorage.setItemAsync !== "function") {
|
|
879
|
+
console.error(`this.clientStorage.setItemAsync is not a function`);
|
|
880
|
+
break;
|
|
881
|
+
}
|
|
882
|
+
this.clientStorage.setItemAsync(key, value);
|
|
883
|
+
break;
|
|
884
|
+
}
|
|
885
|
+
}
|
|
886
|
+
async _getItem(key) {
|
|
887
|
+
switch (this.clientStorageType) {
|
|
888
|
+
case "web":
|
|
889
|
+
if (typeof this.clientStorage.getItem !== "function") {
|
|
890
|
+
console.error(`this.clientStorage.getItem is not a function`);
|
|
891
|
+
break;
|
|
892
|
+
}
|
|
893
|
+
return this.clientStorage.getItem(key);
|
|
894
|
+
case "custom":
|
|
895
|
+
case "react-native":
|
|
896
|
+
if (typeof this.clientStorage.getItem !== "function") {
|
|
897
|
+
console.error(`this.clientStorage.getItem is not a function`);
|
|
898
|
+
break;
|
|
899
|
+
}
|
|
900
|
+
return await this.clientStorage.getItem(key);
|
|
901
|
+
case "capacitor":
|
|
902
|
+
if (typeof this.clientStorage.get !== "function") {
|
|
903
|
+
console.error(`this.clientStorage.get is not a function`);
|
|
904
|
+
break;
|
|
905
|
+
}
|
|
906
|
+
const res = await this.clientStorage.get({ key });
|
|
907
|
+
return res.value;
|
|
908
|
+
case "expo-secure-storage":
|
|
909
|
+
if (typeof this.clientStorage.getItemAsync !== "function") {
|
|
910
|
+
console.error(`this.clientStorage.getItemAsync is not a function`);
|
|
911
|
+
break;
|
|
912
|
+
}
|
|
913
|
+
return this.clientStorage.getItemAsync(key);
|
|
914
|
+
default:
|
|
915
|
+
return "";
|
|
916
|
+
}
|
|
917
|
+
return "";
|
|
918
|
+
}
|
|
919
|
+
async _removeItem(key) {
|
|
920
|
+
switch (this.clientStorageType) {
|
|
921
|
+
case "web":
|
|
922
|
+
if (typeof this.clientStorage.removeItem !== "function") {
|
|
923
|
+
console.error(`this.clientStorage.removeItem is not a function`);
|
|
924
|
+
break;
|
|
925
|
+
}
|
|
926
|
+
return void this.clientStorage.removeItem(key);
|
|
927
|
+
case "custom":
|
|
928
|
+
case "react-native":
|
|
929
|
+
if (typeof this.clientStorage.removeItem !== "function") {
|
|
930
|
+
console.error(`this.clientStorage.removeItem is not a function`);
|
|
931
|
+
break;
|
|
932
|
+
}
|
|
933
|
+
return void this.clientStorage.removeItem(key);
|
|
934
|
+
case "capacitor":
|
|
935
|
+
if (typeof this.clientStorage.remove !== "function") {
|
|
936
|
+
console.error(`this.clientStorage.remove is not a function`);
|
|
937
|
+
break;
|
|
938
|
+
}
|
|
939
|
+
await this.clientStorage.remove({ key });
|
|
940
|
+
break;
|
|
941
|
+
case "expo-secure-storage":
|
|
942
|
+
if (typeof this.clientStorage.deleteItemAsync !== "function") {
|
|
943
|
+
console.error(`this.clientStorage.deleteItemAsync is not a function`);
|
|
944
|
+
break;
|
|
945
|
+
}
|
|
946
|
+
this.clientStorage.deleteItemAsync(key);
|
|
947
|
+
break;
|
|
948
|
+
}
|
|
949
|
+
}
|
|
950
|
+
_autoLogin(refreshToken) {
|
|
951
|
+
if (!isBrowser()) {
|
|
952
|
+
return;
|
|
953
|
+
}
|
|
954
|
+
this._refreshTokens(refreshToken);
|
|
955
|
+
}
|
|
956
|
+
async _refreshTokens(paramRefreshToken) {
|
|
957
|
+
const refreshToken = paramRefreshToken || await this._getItem(NHOST_REFRESH_TOKEN);
|
|
958
|
+
if (!refreshToken) {
|
|
959
|
+
setTimeout(async () => {
|
|
960
|
+
await this._clearSession();
|
|
961
|
+
}, 0);
|
|
962
|
+
return;
|
|
963
|
+
}
|
|
964
|
+
try {
|
|
965
|
+
const { session, error } = await this.api.refreshToken({ refreshToken });
|
|
966
|
+
if (error && error.status === 401) {
|
|
967
|
+
await this._clearSession();
|
|
968
|
+
return;
|
|
969
|
+
}
|
|
970
|
+
if (!session)
|
|
971
|
+
throw new Error("Invalid session data");
|
|
972
|
+
await this._setSession(session);
|
|
973
|
+
this.tokenChanged();
|
|
974
|
+
} catch {
|
|
975
|
+
}
|
|
976
|
+
}
|
|
977
|
+
tokenChanged() {
|
|
978
|
+
for (const tokenChangedFunction of this.onTokenChangedFunctions) {
|
|
979
|
+
tokenChangedFunction(this.session);
|
|
980
|
+
}
|
|
981
|
+
}
|
|
982
|
+
authStateChanged({
|
|
983
|
+
event,
|
|
984
|
+
session
|
|
985
|
+
}) {
|
|
986
|
+
if (event === "SIGNED_IN" && session) {
|
|
987
|
+
this.api.setAccessToken(session.accessToken);
|
|
988
|
+
} else {
|
|
989
|
+
this.api.setAccessToken(void 0);
|
|
990
|
+
}
|
|
991
|
+
for (const authChangedFunction of this.onAuthChangedFunctions) {
|
|
992
|
+
authChangedFunction(event, session);
|
|
993
|
+
}
|
|
994
|
+
}
|
|
995
|
+
async _clearSession() {
|
|
996
|
+
const { isLoading, isAuthenticated } = this.getAuthenticationStatus();
|
|
997
|
+
this.session = null;
|
|
998
|
+
this.initAuthLoading = false;
|
|
999
|
+
await this._removeItem(NHOST_REFRESH_TOKEN);
|
|
1000
|
+
if (isLoading || isAuthenticated) {
|
|
1001
|
+
clearInterval(this.refreshInterval);
|
|
1002
|
+
clearInterval(this.refreshSleepCheckInterval);
|
|
1003
|
+
this.authStateChanged({ event: "SIGNED_OUT", session: null });
|
|
1004
|
+
}
|
|
1005
|
+
}
|
|
1006
|
+
async _setSession(session) {
|
|
1007
|
+
const { isAuthenticated } = this.getAuthenticationStatus();
|
|
1008
|
+
this.session = session;
|
|
1009
|
+
await this._setItem(NHOST_REFRESH_TOKEN, session.refreshToken);
|
|
1010
|
+
if (this.autoRefreshToken && !isAuthenticated) {
|
|
1011
|
+
const JWTExpiresIn = session.accessTokenExpiresIn;
|
|
1012
|
+
const refreshIntervalTime = this.refreshIntervalTime ? this.refreshIntervalTime : Math.max(1, JWTExpiresIn - 1);
|
|
1013
|
+
this.refreshInterval = setInterval(async () => {
|
|
1014
|
+
const refreshToken = await this._getItem(NHOST_REFRESH_TOKEN);
|
|
1015
|
+
await this._refreshTokens(refreshToken);
|
|
1016
|
+
}, refreshIntervalTime * 1e3);
|
|
1017
|
+
this.refreshIntervalSleepCheckLastSample = Date.now();
|
|
1018
|
+
this.refreshSleepCheckInterval = setInterval(async () => {
|
|
1019
|
+
if (Date.now() - this.refreshIntervalSleepCheckLastSample >= this.sampleRate * 2) {
|
|
1020
|
+
const refreshToken = await this._getItem(NHOST_REFRESH_TOKEN);
|
|
1021
|
+
await this._refreshTokens(refreshToken);
|
|
1022
|
+
}
|
|
1023
|
+
this.refreshIntervalSleepCheckLastSample = Date.now();
|
|
1024
|
+
}, this.sampleRate);
|
|
1025
|
+
this.authStateChanged({ event: "SIGNED_IN", session: this.session });
|
|
1026
|
+
}
|
|
1027
|
+
this.initAuthLoading = false;
|
|
1028
|
+
}
|
|
1029
|
+
}
|
|
1030
|
+
const axios$2 = require("axios");
|
|
1031
|
+
class HasuraStorageApi {
|
|
1032
|
+
constructor({ url }) {
|
|
1033
|
+
this.url = url;
|
|
1034
|
+
this.httpClient = axios$2.create({
|
|
1035
|
+
baseURL: this.url
|
|
1036
|
+
});
|
|
1037
|
+
}
|
|
1038
|
+
async upload(params) {
|
|
1039
|
+
try {
|
|
1040
|
+
const res = await this.httpClient.post("/files", params.file, {
|
|
1041
|
+
headers: __spreadValues(__spreadValues({}, this.generateUploadHeaders(params)), this.generateAuthHeaders())
|
|
1042
|
+
});
|
|
1043
|
+
return { fileMetadata: res.data, error: null };
|
|
1044
|
+
} catch (error) {
|
|
1045
|
+
return { fileMetadata: null, error };
|
|
1046
|
+
}
|
|
1047
|
+
}
|
|
1048
|
+
async getPresignedUrl(params) {
|
|
1049
|
+
try {
|
|
1050
|
+
const { fileId } = params;
|
|
1051
|
+
const res = await this.httpClient.get(`/files/${fileId}/presignedurl`, {
|
|
1052
|
+
headers: __spreadValues({}, this.generateAuthHeaders())
|
|
1053
|
+
});
|
|
1054
|
+
return { presignedUrl: res.data, error: null };
|
|
1055
|
+
} catch (error) {
|
|
1056
|
+
return { presignedUrl: null, error };
|
|
1057
|
+
}
|
|
1058
|
+
}
|
|
1059
|
+
async delete(params) {
|
|
1060
|
+
try {
|
|
1061
|
+
const { fileId } = params;
|
|
1062
|
+
await this.httpClient.delete(`/files/${fileId}`, {
|
|
1063
|
+
headers: __spreadValues({}, this.generateAuthHeaders())
|
|
1064
|
+
});
|
|
1065
|
+
return { error: null };
|
|
1066
|
+
} catch (error) {
|
|
1067
|
+
return { error };
|
|
1068
|
+
}
|
|
1069
|
+
}
|
|
1070
|
+
setAccessToken(accessToken) {
|
|
1071
|
+
this.accessToken = accessToken;
|
|
1072
|
+
}
|
|
1073
|
+
generateUploadHeaders(params) {
|
|
1074
|
+
const { bucketId, name, id } = params;
|
|
1075
|
+
const uploadheaders = {};
|
|
1076
|
+
if (bucketId) {
|
|
1077
|
+
uploadheaders["x-nhost-bucket-id"] = bucketId;
|
|
1078
|
+
}
|
|
1079
|
+
if (id) {
|
|
1080
|
+
uploadheaders["x-nhost-file-id"] = id;
|
|
1081
|
+
}
|
|
1082
|
+
if (name) {
|
|
1083
|
+
uploadheaders["x-nhost-file-name"] = name;
|
|
1084
|
+
}
|
|
1085
|
+
return uploadheaders;
|
|
1086
|
+
}
|
|
1087
|
+
generateAuthHeaders() {
|
|
1088
|
+
if (!this.accessToken) {
|
|
1089
|
+
return null;
|
|
1090
|
+
}
|
|
1091
|
+
return {
|
|
1092
|
+
Authorization: `Bearer ${this.accessToken}`
|
|
1093
|
+
};
|
|
1094
|
+
}
|
|
1095
|
+
}
|
|
1096
|
+
class HasuraStorageClient {
|
|
1097
|
+
constructor({ url }) {
|
|
1098
|
+
this.url = url;
|
|
1099
|
+
this.api = new HasuraStorageApi({ url });
|
|
1100
|
+
}
|
|
1101
|
+
async upload(params) {
|
|
1102
|
+
const file = new FormData();
|
|
1103
|
+
file.append("file", params.file);
|
|
1104
|
+
const { fileMetadata, error } = await this.api.upload(__spreadProps(__spreadValues({}, params), {
|
|
1105
|
+
file
|
|
1106
|
+
}));
|
|
1107
|
+
if (error) {
|
|
1108
|
+
return { fileMetadata: null, error };
|
|
1109
|
+
}
|
|
1110
|
+
if (!fileMetadata) {
|
|
1111
|
+
return { fileMetadata: null, error: new Error("Invalid file returned") };
|
|
1112
|
+
}
|
|
1113
|
+
return { fileMetadata, error: null };
|
|
1114
|
+
}
|
|
1115
|
+
getUrl(params) {
|
|
1116
|
+
const { fileId } = params;
|
|
1117
|
+
return `${this.url}/files/${fileId}`;
|
|
1118
|
+
}
|
|
1119
|
+
async getPresignedUrl(params) {
|
|
1120
|
+
const { presignedUrl, error } = await this.api.getPresignedUrl(params);
|
|
1121
|
+
if (error) {
|
|
1122
|
+
return { presignedUrl: null, error };
|
|
1123
|
+
}
|
|
1124
|
+
if (!presignedUrl) {
|
|
1125
|
+
return { presignedUrl: null, error: new Error("Invalid file id") };
|
|
1126
|
+
}
|
|
1127
|
+
return { presignedUrl, error: null };
|
|
1128
|
+
}
|
|
1129
|
+
async delete(params) {
|
|
1130
|
+
const { error } = await this.api.delete(params);
|
|
1131
|
+
if (error) {
|
|
1132
|
+
return { error };
|
|
1133
|
+
}
|
|
1134
|
+
return { error: null };
|
|
1135
|
+
}
|
|
1136
|
+
setAccessToken(accessToken) {
|
|
1137
|
+
this.api.setAccessToken(accessToken);
|
|
1138
|
+
}
|
|
1139
|
+
}
|
|
1140
|
+
const axios$1 = require("axios");
|
|
1141
|
+
class NhostFunctionsClient {
|
|
1142
|
+
constructor(params) {
|
|
1143
|
+
const { url } = params;
|
|
1144
|
+
this.accessToken = null;
|
|
1145
|
+
this.instance = axios$1.create({
|
|
1146
|
+
baseURL: url
|
|
1147
|
+
});
|
|
1148
|
+
}
|
|
1149
|
+
async call(url, data, config) {
|
|
1150
|
+
const headers = __spreadValues(__spreadValues({}, this.generateAccessTokenHeaders()), config == null ? void 0 : config.headers);
|
|
1151
|
+
let res;
|
|
1152
|
+
try {
|
|
1153
|
+
res = await this.instance.post(url, data, __spreadProps(__spreadValues({}, config), { headers }));
|
|
1154
|
+
} catch (error) {
|
|
1155
|
+
if (error instanceof Error) {
|
|
1156
|
+
return { res: null, error };
|
|
1157
|
+
}
|
|
1158
|
+
}
|
|
1159
|
+
if (!res) {
|
|
1160
|
+
return {
|
|
1161
|
+
res: null,
|
|
1162
|
+
error: new Error("Unable to make post request to funtion")
|
|
1163
|
+
};
|
|
1164
|
+
}
|
|
1165
|
+
return { res, error: null };
|
|
1166
|
+
}
|
|
1167
|
+
setAccessToken(accessToken) {
|
|
1168
|
+
if (!accessToken) {
|
|
1169
|
+
this.accessToken = null;
|
|
1170
|
+
return;
|
|
1171
|
+
}
|
|
1172
|
+
this.accessToken = accessToken;
|
|
1173
|
+
}
|
|
1174
|
+
generateAccessTokenHeaders() {
|
|
1175
|
+
if (!this.accessToken) {
|
|
1176
|
+
return;
|
|
1177
|
+
}
|
|
1178
|
+
return {
|
|
1179
|
+
Authorization: `Bearer ${this.accessToken}`
|
|
1180
|
+
};
|
|
1181
|
+
}
|
|
1182
|
+
}
|
|
1183
|
+
const axios = require("axios");
|
|
1184
|
+
class NhostGraphqlClient {
|
|
1185
|
+
constructor(params) {
|
|
1186
|
+
const { url } = params;
|
|
1187
|
+
this.url = url;
|
|
1188
|
+
this.accessToken = null;
|
|
1189
|
+
this.instance = axios.create({
|
|
1190
|
+
baseURL: url
|
|
1191
|
+
});
|
|
1192
|
+
}
|
|
1193
|
+
async request(document, variables, config) {
|
|
1194
|
+
const headers = __spreadValues(__spreadValues({}, config == null ? void 0 : config.headers), this.generateAccessTokenHeaders());
|
|
1195
|
+
const operationName = "";
|
|
1196
|
+
let responseData;
|
|
1197
|
+
try {
|
|
1198
|
+
const res = await this.instance.post("", {
|
|
1199
|
+
operationName: operationName || void 0,
|
|
1200
|
+
query: document,
|
|
1201
|
+
variables
|
|
1202
|
+
}, __spreadProps(__spreadValues({}, config), { headers }));
|
|
1203
|
+
responseData = res.data;
|
|
1204
|
+
} catch (error) {
|
|
1205
|
+
if (error instanceof Error) {
|
|
1206
|
+
return { data: null, error };
|
|
1207
|
+
}
|
|
1208
|
+
console.error(error);
|
|
1209
|
+
return {
|
|
1210
|
+
data: null,
|
|
1211
|
+
error: new Error("Unable to get do GraphQL request")
|
|
1212
|
+
};
|
|
1213
|
+
}
|
|
1214
|
+
if (typeof responseData !== "object" || Array.isArray(responseData) || responseData === null) {
|
|
1215
|
+
return {
|
|
1216
|
+
data: null,
|
|
1217
|
+
error: new Error("incorrect response data from GraphQL server")
|
|
1218
|
+
};
|
|
1219
|
+
}
|
|
1220
|
+
responseData = responseData;
|
|
1221
|
+
if (responseData.errors) {
|
|
1222
|
+
return {
|
|
1223
|
+
data: null,
|
|
1224
|
+
error: responseData.errors
|
|
1225
|
+
};
|
|
1226
|
+
}
|
|
1227
|
+
return { data: responseData.data, error: null };
|
|
1228
|
+
}
|
|
1229
|
+
getUrl() {
|
|
1230
|
+
return this.url;
|
|
1231
|
+
}
|
|
1232
|
+
setAccessToken(accessToken) {
|
|
1233
|
+
if (!accessToken) {
|
|
1234
|
+
this.accessToken = null;
|
|
1235
|
+
return;
|
|
1236
|
+
}
|
|
1237
|
+
this.accessToken = accessToken;
|
|
1238
|
+
}
|
|
1239
|
+
generateAccessTokenHeaders() {
|
|
1240
|
+
if (!this.accessToken) {
|
|
1241
|
+
return;
|
|
1242
|
+
}
|
|
1243
|
+
return {
|
|
1244
|
+
Authorization: `Bearer ${this.accessToken}`
|
|
1245
|
+
};
|
|
1246
|
+
}
|
|
1247
|
+
}
|
|
1248
|
+
class NhostClient {
|
|
1249
|
+
constructor(params) {
|
|
1250
|
+
if (!params.backendUrl)
|
|
1251
|
+
throw new Error("Please specify a `backendUrl`. Docs: [todo]!");
|
|
1252
|
+
const {
|
|
1253
|
+
backendUrl,
|
|
1254
|
+
refreshIntervalTime,
|
|
1255
|
+
clientStorage,
|
|
1256
|
+
clientStorageType,
|
|
1257
|
+
autoRefreshToken,
|
|
1258
|
+
autoLogin
|
|
1259
|
+
} = params;
|
|
1260
|
+
this.auth = new HasuraAuthClient({
|
|
1261
|
+
url: `${backendUrl}/v1/auth`,
|
|
1262
|
+
refreshIntervalTime,
|
|
1263
|
+
clientStorage,
|
|
1264
|
+
clientStorageType,
|
|
1265
|
+
autoRefreshToken,
|
|
1266
|
+
autoLogin
|
|
1267
|
+
});
|
|
1268
|
+
this.storage = new HasuraStorageClient({
|
|
1269
|
+
url: `${backendUrl}/v1/storage`
|
|
1270
|
+
});
|
|
1271
|
+
this.functions = new NhostFunctionsClient({
|
|
1272
|
+
url: `${backendUrl}/v1/functions`
|
|
1273
|
+
});
|
|
1274
|
+
this.graphql = new NhostGraphqlClient({
|
|
1275
|
+
url: `${backendUrl}/v1/graphql`
|
|
1276
|
+
});
|
|
1277
|
+
this.storage.setAccessToken(this.auth.getAccessToken());
|
|
1278
|
+
this.functions.setAccessToken(this.auth.getAccessToken());
|
|
1279
|
+
this.graphql.setAccessToken(this.auth.getAccessToken());
|
|
1280
|
+
this.auth.onAuthStateChanged((_event, session) => {
|
|
1281
|
+
this.storage.setAccessToken(session == null ? void 0 : session.accessToken);
|
|
1282
|
+
this.functions.setAccessToken(session == null ? void 0 : session.accessToken);
|
|
1283
|
+
this.graphql.setAccessToken(session == null ? void 0 : session.accessToken);
|
|
1284
|
+
});
|
|
1285
|
+
this.auth.onTokenChanged((session) => {
|
|
1286
|
+
this.storage.setAccessToken(session == null ? void 0 : session.accessToken);
|
|
1287
|
+
this.functions.setAccessToken(session == null ? void 0 : session.accessToken);
|
|
1288
|
+
this.graphql.setAccessToken(session == null ? void 0 : session.accessToken);
|
|
1289
|
+
});
|
|
1290
|
+
}
|
|
1291
|
+
}
|
|
1292
|
+
const createClient = (config) => new NhostClient(config);
|
|
1293
|
+
export { NhostClient, createClient };
|