skapi-js 0.2.0 → 0.2.2
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/dist/skapi.js +1 -1
- package/dist/skapi.js.map +1 -1
- package/dist/skapi.module.js +1 -1
- package/dist/skapi.module.js.map +1 -1
- package/js/Main.d.ts +3 -0
- package/js/Main.js +3 -0
- package/js/Types.d.ts +194 -0
- package/js/Types.js +1 -0
- package/js/main/error.d.ts +8 -0
- package/js/main/error.js +38 -0
- package/js/main/skapi.d.ts +79 -0
- package/js/main/skapi.js +285 -0
- package/js/methods/database.d.ts +83 -0
- package/js/methods/database.js +915 -0
- package/js/methods/request.d.ts +36 -0
- package/js/methods/request.js +656 -0
- package/js/methods/subscription.d.ts +46 -0
- package/js/methods/subscription.js +240 -0
- package/js/methods/user.d.ts +67 -0
- package/js/methods/user.js +747 -0
- package/js/utils/utils.d.ts +20 -0
- package/js/utils/utils.js +286 -0
- package/js/utils/validator.d.ts +19 -0
- package/js/utils/validator.js +294 -0
- package/package.json +5 -3
package/js/Main.d.ts
ADDED
package/js/Main.js
ADDED
package/js/Types.d.ts
ADDED
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
export type Condition = 'gt' | 'gte' | 'lt' | 'lte' | 'eq' | 'ne' | '>' | '>=' | '<' | '<=' | '=' | '!=';
|
|
2
|
+
export type SubscriptionGroup<T> = {
|
|
3
|
+
user_id: string;
|
|
4
|
+
group?: T;
|
|
5
|
+
};
|
|
6
|
+
export type Database<Tbl, Ref, Idx> = {
|
|
7
|
+
service?: string;
|
|
8
|
+
record_id?: string;
|
|
9
|
+
table?: Tbl;
|
|
10
|
+
reference?: Ref;
|
|
11
|
+
index?: Idx;
|
|
12
|
+
private_access_key?: string;
|
|
13
|
+
};
|
|
14
|
+
export type GetRecordQuery = Database<{
|
|
15
|
+
name: string;
|
|
16
|
+
access_group?: number | 'private' | 'public' | 'authorized';
|
|
17
|
+
subscription?: {
|
|
18
|
+
user_id: string;
|
|
19
|
+
group: number;
|
|
20
|
+
};
|
|
21
|
+
}, string, {
|
|
22
|
+
name: string | '$updated' | '$uploaded' | '$referenced_count' | '$user_id';
|
|
23
|
+
value: string | number | boolean;
|
|
24
|
+
condition?: Condition;
|
|
25
|
+
range?: string | number | boolean;
|
|
26
|
+
}> & {
|
|
27
|
+
tag?: string;
|
|
28
|
+
};
|
|
29
|
+
export type PostRecordConfig = Database<{
|
|
30
|
+
name?: string;
|
|
31
|
+
access_group?: number | 'private' | 'public' | 'authorized';
|
|
32
|
+
subscription_group?: number;
|
|
33
|
+
}, {
|
|
34
|
+
record_id: string;
|
|
35
|
+
reference_limit: number | null;
|
|
36
|
+
allow_multiple_reference: boolean;
|
|
37
|
+
}, {
|
|
38
|
+
name: string;
|
|
39
|
+
value: string | number | boolean;
|
|
40
|
+
} | null> & {
|
|
41
|
+
tags?: string[];
|
|
42
|
+
};
|
|
43
|
+
export type RecordData = {
|
|
44
|
+
service: string;
|
|
45
|
+
record_id: string;
|
|
46
|
+
user_id: string;
|
|
47
|
+
updated: number;
|
|
48
|
+
uploaded: number;
|
|
49
|
+
table: {
|
|
50
|
+
name: string;
|
|
51
|
+
access_group?: number | 'private' | 'public' | 'authorized';
|
|
52
|
+
subscription?: {
|
|
53
|
+
user_id: string;
|
|
54
|
+
group: number;
|
|
55
|
+
};
|
|
56
|
+
};
|
|
57
|
+
reference: {
|
|
58
|
+
record_id?: string;
|
|
59
|
+
reference_limit: number;
|
|
60
|
+
allow_multiple_reference: boolean;
|
|
61
|
+
referenced_count: number;
|
|
62
|
+
};
|
|
63
|
+
index?: {
|
|
64
|
+
name: string;
|
|
65
|
+
value: string | number | boolean;
|
|
66
|
+
};
|
|
67
|
+
data?: Record<string, any>;
|
|
68
|
+
tags?: string[];
|
|
69
|
+
ip: string;
|
|
70
|
+
};
|
|
71
|
+
export type Connection = {
|
|
72
|
+
locale: string;
|
|
73
|
+
owner: string;
|
|
74
|
+
email: string;
|
|
75
|
+
service: string;
|
|
76
|
+
region: string;
|
|
77
|
+
timestamp: number;
|
|
78
|
+
ip: string;
|
|
79
|
+
};
|
|
80
|
+
export type FormSubmitCallback = {
|
|
81
|
+
response?(response: any): any;
|
|
82
|
+
onerror?(error: Error): any;
|
|
83
|
+
formData?(formData: FormData): Promise<FormData> | FormData;
|
|
84
|
+
progress: ProgressCallback;
|
|
85
|
+
};
|
|
86
|
+
export type Form<T> = HTMLFormElement | FormData | SubmitEvent | T;
|
|
87
|
+
export type Newsletters = {
|
|
88
|
+
message_id: string;
|
|
89
|
+
timestamp: number;
|
|
90
|
+
complaint: number;
|
|
91
|
+
read: number;
|
|
92
|
+
subject: string;
|
|
93
|
+
bounced: string;
|
|
94
|
+
url: string;
|
|
95
|
+
};
|
|
96
|
+
export type UserAttributes = {
|
|
97
|
+
name?: string;
|
|
98
|
+
email?: string;
|
|
99
|
+
phone_number?: string;
|
|
100
|
+
address?: string | {
|
|
101
|
+
formatted: string;
|
|
102
|
+
locality: string;
|
|
103
|
+
region: string;
|
|
104
|
+
postal_code: string;
|
|
105
|
+
country: string;
|
|
106
|
+
};
|
|
107
|
+
gender?: string;
|
|
108
|
+
birthdate?: string;
|
|
109
|
+
email_public?: boolean;
|
|
110
|
+
phone_number_public?: boolean;
|
|
111
|
+
address_public?: boolean;
|
|
112
|
+
gender_public?: boolean;
|
|
113
|
+
birthdate_public?: boolean;
|
|
114
|
+
misc?: string;
|
|
115
|
+
};
|
|
116
|
+
export type UserProfile = {
|
|
117
|
+
service: string;
|
|
118
|
+
owner?: string;
|
|
119
|
+
access_group?: number;
|
|
120
|
+
user_id: string;
|
|
121
|
+
locale: string;
|
|
122
|
+
email_verified?: boolean;
|
|
123
|
+
phone_number_verified?: boolean;
|
|
124
|
+
signup_ticket?: string;
|
|
125
|
+
} & UserAttributes;
|
|
126
|
+
export interface User extends UserProfile {
|
|
127
|
+
subscribers: number;
|
|
128
|
+
timestamp: number;
|
|
129
|
+
}
|
|
130
|
+
export type QueryParams = {
|
|
131
|
+
searchFor: string;
|
|
132
|
+
value: string | number | boolean;
|
|
133
|
+
condition?: '>' | '>=' | '=' | '<' | '<=' | '!=' | 'gt' | 'gte' | 'eq' | 'lt' | 'lte' | 'ne';
|
|
134
|
+
range?: string | number | boolean;
|
|
135
|
+
};
|
|
136
|
+
export type ProgressCallback = (e: {
|
|
137
|
+
status: 'upload' | 'download';
|
|
138
|
+
progress: number;
|
|
139
|
+
loaded: number;
|
|
140
|
+
total: number;
|
|
141
|
+
currentFile?: File;
|
|
142
|
+
completed?: File[];
|
|
143
|
+
failed?: File[];
|
|
144
|
+
abort: () => void;
|
|
145
|
+
}) => void;
|
|
146
|
+
export type FetchOptions = {
|
|
147
|
+
limit?: number;
|
|
148
|
+
fetchMore?: boolean;
|
|
149
|
+
ascending?: boolean;
|
|
150
|
+
startKey?: {
|
|
151
|
+
[key: string]: any;
|
|
152
|
+
};
|
|
153
|
+
progress?: ProgressCallback;
|
|
154
|
+
};
|
|
155
|
+
export type DatabaseResponse<T> = {
|
|
156
|
+
list: T[];
|
|
157
|
+
startKey: string;
|
|
158
|
+
endOfList: boolean;
|
|
159
|
+
startKeyHistory: string[];
|
|
160
|
+
};
|
|
161
|
+
export type Service = {
|
|
162
|
+
active: number;
|
|
163
|
+
api_key: string;
|
|
164
|
+
cors: string[];
|
|
165
|
+
email: string;
|
|
166
|
+
email_subscribers: number;
|
|
167
|
+
group: number;
|
|
168
|
+
region: string;
|
|
169
|
+
name: string;
|
|
170
|
+
newsletter_subscribers: number;
|
|
171
|
+
service: string;
|
|
172
|
+
template_activation: {
|
|
173
|
+
html: string;
|
|
174
|
+
subject: string;
|
|
175
|
+
};
|
|
176
|
+
template_verification: {
|
|
177
|
+
html: string;
|
|
178
|
+
sms: string;
|
|
179
|
+
subject: string;
|
|
180
|
+
};
|
|
181
|
+
template_welcome: {
|
|
182
|
+
html: string;
|
|
183
|
+
subject: string;
|
|
184
|
+
};
|
|
185
|
+
timestamp: number;
|
|
186
|
+
triggers: {
|
|
187
|
+
newsletter_signed: string;
|
|
188
|
+
newsletter_subscribers: string;
|
|
189
|
+
template_activation: string;
|
|
190
|
+
template_verification: string;
|
|
191
|
+
template_welcome: string;
|
|
192
|
+
};
|
|
193
|
+
users: number;
|
|
194
|
+
};
|
package/js/Types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/js/main/error.js
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
export default class SkapiError extends Error {
|
|
2
|
+
constructor(error, options) {
|
|
3
|
+
if (Array.isArray(error) && error.length <= 2) {
|
|
4
|
+
super(error[1] || 'Something went wrong.');
|
|
5
|
+
this.name = "SKAPI";
|
|
6
|
+
this.code = error[0] || "ERROR";
|
|
7
|
+
if (options) {
|
|
8
|
+
if (options.code) {
|
|
9
|
+
this.code = options.code;
|
|
10
|
+
}
|
|
11
|
+
if (options.cause) {
|
|
12
|
+
this.cause = options.cause;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
else if (typeof error === 'string') {
|
|
17
|
+
super(error || 'Something went wrong.');
|
|
18
|
+
this.name = "SKAPI";
|
|
19
|
+
this.code = 'ERROR';
|
|
20
|
+
if (options) {
|
|
21
|
+
if (options.code) {
|
|
22
|
+
this.code = options.code;
|
|
23
|
+
}
|
|
24
|
+
if (options.cause) {
|
|
25
|
+
this.cause = options.cause;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
else if (error instanceof Error) {
|
|
30
|
+
super(error.message || 'Something went wrong.');
|
|
31
|
+
this.cause = error;
|
|
32
|
+
this.name = error.name;
|
|
33
|
+
if (error.hasOwnProperty('code')) {
|
|
34
|
+
this.code = error.code;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { User, Connection } from '../Types';
|
|
2
|
+
export default class Skapi {
|
|
3
|
+
version: string;
|
|
4
|
+
private __disabledAccount;
|
|
5
|
+
private __cached_requests;
|
|
6
|
+
private __startKeyHistory;
|
|
7
|
+
private __request_signup_confirmation;
|
|
8
|
+
service: string;
|
|
9
|
+
owner: string;
|
|
10
|
+
private __class_properties_has_been_cached;
|
|
11
|
+
session: Record<string, any> | null;
|
|
12
|
+
__user: User | null;
|
|
13
|
+
get user(): User | null;
|
|
14
|
+
set user(value: User | null);
|
|
15
|
+
connection: Connection | null;
|
|
16
|
+
host: string;
|
|
17
|
+
hostDomain: string;
|
|
18
|
+
admin_endpoint: Promise<Record<string, any>>;
|
|
19
|
+
record_endpoint: Promise<Record<string, any>>;
|
|
20
|
+
validate: {
|
|
21
|
+
userId(val: string): boolean;
|
|
22
|
+
url(val: string | string[]): boolean;
|
|
23
|
+
phoneNumber(val: string): boolean;
|
|
24
|
+
birthdate(val: string): boolean;
|
|
25
|
+
email(val: string): boolean;
|
|
26
|
+
};
|
|
27
|
+
__connection: Promise<Connection | null>;
|
|
28
|
+
constructor(service_id: string, owner: string, options?: {
|
|
29
|
+
autoLogin: boolean;
|
|
30
|
+
});
|
|
31
|
+
updateConnection(): Promise<Connection>;
|
|
32
|
+
getConnection: any;
|
|
33
|
+
getProfile: any;
|
|
34
|
+
checkAdmin: any;
|
|
35
|
+
getFile: any;
|
|
36
|
+
request: any;
|
|
37
|
+
secureRequest: any;
|
|
38
|
+
getFormResponse: any;
|
|
39
|
+
getRecords: any;
|
|
40
|
+
getTables: any;
|
|
41
|
+
getIndexes: any;
|
|
42
|
+
getTags: any;
|
|
43
|
+
deleteRecords: any;
|
|
44
|
+
resendSignupConfirmation: any;
|
|
45
|
+
recoverAccount: any;
|
|
46
|
+
getUsers: any;
|
|
47
|
+
disableAccount: any;
|
|
48
|
+
lastVerifiedEmail: any;
|
|
49
|
+
getSubscribedTo: any;
|
|
50
|
+
getSubscribers: any;
|
|
51
|
+
getSubscriptions: any;
|
|
52
|
+
unsubscribeNewsletter: any;
|
|
53
|
+
getNewsletters: any;
|
|
54
|
+
getNewsletterSubscription: any;
|
|
55
|
+
requestUsernameChange: any;
|
|
56
|
+
grantPrivateRecordAccess: any;
|
|
57
|
+
removePrivateRecordAccess: any;
|
|
58
|
+
listPrivateRecordAccess: any;
|
|
59
|
+
requestPrivateRecordAccessKey: any;
|
|
60
|
+
listHostDirectory: any;
|
|
61
|
+
deleteFiles: any;
|
|
62
|
+
uploadFiles(...args: any[]): any;
|
|
63
|
+
mock(...args: any[]): any;
|
|
64
|
+
login(...args: any[]): any;
|
|
65
|
+
logout(...args: any[]): any;
|
|
66
|
+
signup(...args: any[]): any;
|
|
67
|
+
resetPassword(...args: any[]): any;
|
|
68
|
+
verifyEmail(...args: any[]): any;
|
|
69
|
+
verifyPhoneNumber(...args: any[]): any;
|
|
70
|
+
forgotPassword(...args: any[]): any;
|
|
71
|
+
changePassword(...args: any[]): any;
|
|
72
|
+
updateProfile(...args: any[]): any;
|
|
73
|
+
postRecord(...args: any[]): any;
|
|
74
|
+
subscribe(...args: any[]): any;
|
|
75
|
+
unsubscribe(...args: any[]): any;
|
|
76
|
+
blockSubscriber(...args: any[]): any;
|
|
77
|
+
unblockSubscriber(...args: any[]): any;
|
|
78
|
+
subscribeNewsletter(...args: any[]): any;
|
|
79
|
+
}
|
package/js/main/skapi.js
ADDED
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
import SkapiError from './error';
|
|
8
|
+
import validator from '../utils/validator';
|
|
9
|
+
import { getRecords, postRecord, deleteRecords, getTables, getIndexes, getTags, uploadFiles, getFile, grantPrivateRecordAccess, removePrivateRecordAccess, listPrivateRecordAccess, requestPrivateRecordAccessKey, deleteFiles } from '../methods/database';
|
|
10
|
+
import { request, secureRequest, mock, getFormResponse, formHandler, getConnection, listHostDirectory } from '../methods/request';
|
|
11
|
+
import { subscribe, unsubscribe, blockSubscriber, unblockSubscriber, getSubscribers, getSubscribedTo, getSubscriptions, subscribeNewsletter, getNewsletters, unsubscribeNewsletter, getNewsletterSubscription } from '../methods/subscription';
|
|
12
|
+
import { checkAdmin, getProfile, logout, recoverAccount, resendSignupConfirmation, authentication, login, signup, disableAccount, resetPassword, verifyEmail, verifyPhoneNumber, forgotPassword, changePassword, updateProfile, getUsers, setUserPool, userPool, lastVerifiedEmail, requestUsernameChange } from '../methods/user';
|
|
13
|
+
export default class Skapi {
|
|
14
|
+
get user() {
|
|
15
|
+
if (this.__user && Object.keys(this.__user).length) {
|
|
16
|
+
return JSON.parse(JSON.stringify(this.__user));
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
set user(value) {
|
|
23
|
+
}
|
|
24
|
+
constructor(service_id, owner, options) {
|
|
25
|
+
this.version = '0.2.2';
|
|
26
|
+
this.__disabledAccount = null;
|
|
27
|
+
this.__cached_requests = {};
|
|
28
|
+
this.__startKeyHistory = {};
|
|
29
|
+
this.__request_signup_confirmation = null;
|
|
30
|
+
this.__class_properties_has_been_cached = false;
|
|
31
|
+
this.session = null;
|
|
32
|
+
this.__user = null;
|
|
33
|
+
this.connection = null;
|
|
34
|
+
this.host = 'skapi';
|
|
35
|
+
this.hostDomain = 'skapi.com';
|
|
36
|
+
this.validate = {
|
|
37
|
+
userId(val) {
|
|
38
|
+
try {
|
|
39
|
+
validator.UserId(val);
|
|
40
|
+
return true;
|
|
41
|
+
}
|
|
42
|
+
catch (err) {
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
url(val) {
|
|
47
|
+
try {
|
|
48
|
+
validator.Url(val);
|
|
49
|
+
return true;
|
|
50
|
+
}
|
|
51
|
+
catch (err) {
|
|
52
|
+
return false;
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
phoneNumber(val) {
|
|
56
|
+
try {
|
|
57
|
+
validator.PhoneNumber(val);
|
|
58
|
+
return true;
|
|
59
|
+
}
|
|
60
|
+
catch (err) {
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
},
|
|
64
|
+
birthdate(val) {
|
|
65
|
+
try {
|
|
66
|
+
validator.Birthdate(val);
|
|
67
|
+
return true;
|
|
68
|
+
}
|
|
69
|
+
catch (err) {
|
|
70
|
+
return false;
|
|
71
|
+
}
|
|
72
|
+
},
|
|
73
|
+
email(val) {
|
|
74
|
+
try {
|
|
75
|
+
validator.Email(val);
|
|
76
|
+
return true;
|
|
77
|
+
}
|
|
78
|
+
catch (err) {
|
|
79
|
+
return false;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
this.getConnection = getConnection.bind(this);
|
|
84
|
+
this.getProfile = getProfile.bind(this);
|
|
85
|
+
this.checkAdmin = checkAdmin.bind(this);
|
|
86
|
+
this.getFile = getFile.bind(this);
|
|
87
|
+
this.request = request.bind(this);
|
|
88
|
+
this.secureRequest = secureRequest.bind(this);
|
|
89
|
+
this.getFormResponse = getFormResponse.bind(this);
|
|
90
|
+
this.getRecords = getRecords.bind(this);
|
|
91
|
+
this.getTables = getTables.bind(this);
|
|
92
|
+
this.getIndexes = getIndexes.bind(this);
|
|
93
|
+
this.getTags = getTags.bind(this);
|
|
94
|
+
this.deleteRecords = deleteRecords.bind(this);
|
|
95
|
+
this.resendSignupConfirmation = resendSignupConfirmation.bind(this);
|
|
96
|
+
this.recoverAccount = recoverAccount.bind(this);
|
|
97
|
+
this.getUsers = getUsers.bind(this);
|
|
98
|
+
this.disableAccount = disableAccount.bind(this);
|
|
99
|
+
this.lastVerifiedEmail = lastVerifiedEmail.bind(this);
|
|
100
|
+
this.getSubscribedTo = getSubscribedTo.bind(this);
|
|
101
|
+
this.getSubscribers = getSubscribers.bind(this);
|
|
102
|
+
this.getSubscriptions = getSubscriptions.bind(this);
|
|
103
|
+
this.unsubscribeNewsletter = unsubscribeNewsletter.bind(this);
|
|
104
|
+
this.getNewsletters = getNewsletters.bind(this);
|
|
105
|
+
this.getNewsletterSubscription = getNewsletterSubscription.bind(this);
|
|
106
|
+
this.requestUsernameChange = requestUsernameChange.bind(this);
|
|
107
|
+
this.grantPrivateRecordAccess = grantPrivateRecordAccess.bind(this);
|
|
108
|
+
this.removePrivateRecordAccess = removePrivateRecordAccess.bind(this);
|
|
109
|
+
this.listPrivateRecordAccess = listPrivateRecordAccess.bind(this);
|
|
110
|
+
this.requestPrivateRecordAccessKey = requestPrivateRecordAccessKey.bind(this);
|
|
111
|
+
this.listHostDirectory = listHostDirectory.bind(this);
|
|
112
|
+
this.deleteFiles = deleteFiles.bind(this);
|
|
113
|
+
if (typeof service_id !== 'string' || typeof owner !== 'string') {
|
|
114
|
+
throw new SkapiError('"service_id" and "owner" should be type <string>.', { code: 'INVALID_PARAMETER' });
|
|
115
|
+
}
|
|
116
|
+
if (!service_id || !owner) {
|
|
117
|
+
throw new SkapiError('"service_id" and "owner" is required', { code: 'INVALID_PARAMETER' });
|
|
118
|
+
}
|
|
119
|
+
if (owner !== this.host) {
|
|
120
|
+
validator.UserId(owner, '"owner"');
|
|
121
|
+
}
|
|
122
|
+
this.service = service_id;
|
|
123
|
+
this.owner = owner;
|
|
124
|
+
let autoLogin = typeof options?.autoLogin === 'boolean' ? options.autoLogin : true;
|
|
125
|
+
const target_cdn = 'd1h765tqb4s5ov';
|
|
126
|
+
const cdn_domain = `https://${target_cdn}.cloudfront.net`;
|
|
127
|
+
let sreg = service_id.substring(0, 4);
|
|
128
|
+
this.admin_endpoint = fetch(`${cdn_domain}/${sreg}/admin.json`)
|
|
129
|
+
.then(response => response.blob())
|
|
130
|
+
.then(blob => new Promise((resolve, reject) => {
|
|
131
|
+
const reader = new FileReader();
|
|
132
|
+
reader.onloadend = () => resolve(reader.result);
|
|
133
|
+
reader.onerror = reject;
|
|
134
|
+
reader.readAsDataURL(blob);
|
|
135
|
+
}))
|
|
136
|
+
.then(data => typeof data === 'string' ? JSON.parse(window.atob(data.split(',')[1])) : null);
|
|
137
|
+
this.record_endpoint = fetch(`${cdn_domain}/${sreg}/record.json`)
|
|
138
|
+
.then(response => response.blob())
|
|
139
|
+
.then(blob => new Promise((resolve, reject) => {
|
|
140
|
+
const reader = new FileReader();
|
|
141
|
+
reader.onloadend = () => resolve(reader.result);
|
|
142
|
+
reader.onerror = reject;
|
|
143
|
+
reader.readAsDataURL(blob);
|
|
144
|
+
}))
|
|
145
|
+
.then(data => typeof data === 'string' ? JSON.parse(window.atob(data.split(',')[1])) : null);
|
|
146
|
+
this.__connection = (async () => {
|
|
147
|
+
if (!window.sessionStorage) {
|
|
148
|
+
throw new Error(`This browser does not support skapi.`);
|
|
149
|
+
}
|
|
150
|
+
const restore = JSON.parse(window.sessionStorage.getItem(`${service_id}#${owner}`) || 'null');
|
|
151
|
+
if (restore?.connection) {
|
|
152
|
+
for (let k in restore) {
|
|
153
|
+
this[k] = restore[k];
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
const admin_endpoint = await this.admin_endpoint;
|
|
157
|
+
setUserPool({
|
|
158
|
+
UserPoolId: admin_endpoint.userpool_id,
|
|
159
|
+
ClientId: admin_endpoint.userpool_client
|
|
160
|
+
});
|
|
161
|
+
const process = [];
|
|
162
|
+
if (!restore?.connection) {
|
|
163
|
+
process.push(this.updateConnection());
|
|
164
|
+
}
|
|
165
|
+
if (!restore?.connection && !autoLogin) {
|
|
166
|
+
let currentUser = userPool.getCurrentUser();
|
|
167
|
+
if (currentUser) {
|
|
168
|
+
currentUser.signOut();
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
if (restore?.connection || autoLogin) {
|
|
172
|
+
process.push(authentication.bind(this)().getSession({ refreshToken: !restore?.connection }).catch(err => {
|
|
173
|
+
this.__user = null;
|
|
174
|
+
}));
|
|
175
|
+
this.updateConnection();
|
|
176
|
+
}
|
|
177
|
+
let awaitProcess;
|
|
178
|
+
if (process.length) {
|
|
179
|
+
awaitProcess = await Promise.all(process);
|
|
180
|
+
}
|
|
181
|
+
const storeClassProperties = () => {
|
|
182
|
+
if (this.__class_properties_has_been_cached) {
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
let exec = () => {
|
|
186
|
+
let data = {};
|
|
187
|
+
const to_be_cached = [
|
|
188
|
+
'__startKeyHistory',
|
|
189
|
+
'__cached_requests',
|
|
190
|
+
'__request_signup_confirmation',
|
|
191
|
+
'connection',
|
|
192
|
+
];
|
|
193
|
+
if (this.connection) {
|
|
194
|
+
for (let k of to_be_cached) {
|
|
195
|
+
data[k] = this[k];
|
|
196
|
+
}
|
|
197
|
+
window.sessionStorage.setItem(`${service_id}#${owner}`, JSON.stringify(data));
|
|
198
|
+
this.__class_properties_has_been_cached = true;
|
|
199
|
+
}
|
|
200
|
+
};
|
|
201
|
+
return (awaitProcess instanceof Promise) ? awaitProcess.then(() => exec()) : exec();
|
|
202
|
+
};
|
|
203
|
+
window.addEventListener('beforeunload', storeClassProperties);
|
|
204
|
+
window.addEventListener("visibilitychange", storeClassProperties);
|
|
205
|
+
return this.connection;
|
|
206
|
+
})();
|
|
207
|
+
}
|
|
208
|
+
async updateConnection() {
|
|
209
|
+
let skapi = `%c\r\n $$\\ $$\\ \r\n $$ | \\__|\r\n $$$$$$$\\ $$ | $$\\ $$$$$$\\ $$$$$$\\ $$\\ \r\n$$ _____|$$ | $$ |\\____$$\\ $$ __$$\\ $$ |\r\n\\$$$$$$\\ $$$$$$ \/ $$$$$$$ |$$ \/ $$ |$$ |\r\n \\____$$\\ $$ _$$< $$ __$$ |$$ | $$ |$$ |\r\n$$$$$$$ |$$ | \\$$\\\\$$$$$$$ |$$$$$$$ |$$ |\r\n\\_______\/ \\__| \\__|\\_______|$$ ____\/ \\__|\r\n $$ | \r\n $$ | \r\n \\__| \r\n`;
|
|
210
|
+
this.connection = await request.bind(this)('service', {
|
|
211
|
+
service: this.service,
|
|
212
|
+
owner: this.owner
|
|
213
|
+
}, { bypassAwaitConnection: true, method: 'get' });
|
|
214
|
+
console.log(`Built with:\n${skapi}Version: ${this.version}\n\nDocumentation: https://docs.skapi.com`, `font-family: monospace; color:blue;`);
|
|
215
|
+
return this.connection;
|
|
216
|
+
}
|
|
217
|
+
uploadFiles(...args) { return uploadFiles.bind(this)(...args); }
|
|
218
|
+
mock(...args) { return mock.bind(this)(...args); }
|
|
219
|
+
login(...args) { return login.bind(this)(...args); }
|
|
220
|
+
logout(...args) { return logout.bind(this)(...args); }
|
|
221
|
+
signup(...args) { return signup.bind(this)(...args); }
|
|
222
|
+
resetPassword(...args) { return resetPassword.bind(this)(...args); }
|
|
223
|
+
verifyEmail(...args) { return verifyEmail.bind(this)(...args); }
|
|
224
|
+
verifyPhoneNumber(...args) { return verifyPhoneNumber.bind(this)(...args); }
|
|
225
|
+
forgotPassword(...args) { return forgotPassword.bind(this)(...args); }
|
|
226
|
+
changePassword(...args) { return changePassword.bind(this)(...args); }
|
|
227
|
+
updateProfile(...args) { return updateProfile.bind(this)(...args); }
|
|
228
|
+
postRecord(...args) { return postRecord.bind(this)(...args); }
|
|
229
|
+
subscribe(...args) { return subscribe.bind(this)(...args); }
|
|
230
|
+
unsubscribe(...args) { return unsubscribe.bind(this)(...args); }
|
|
231
|
+
blockSubscriber(...args) { return blockSubscriber.bind(this)(...args); }
|
|
232
|
+
unblockSubscriber(...args) { return unblockSubscriber.bind(this)(...args); }
|
|
233
|
+
subscribeNewsletter(...args) { return subscribeNewsletter.bind(this)(...args); }
|
|
234
|
+
}
|
|
235
|
+
__decorate([
|
|
236
|
+
formHandler()
|
|
237
|
+
], Skapi.prototype, "uploadFiles", null);
|
|
238
|
+
__decorate([
|
|
239
|
+
formHandler()
|
|
240
|
+
], Skapi.prototype, "mock", null);
|
|
241
|
+
__decorate([
|
|
242
|
+
formHandler({ preventMultipleCalls: true })
|
|
243
|
+
], Skapi.prototype, "login", null);
|
|
244
|
+
__decorate([
|
|
245
|
+
formHandler()
|
|
246
|
+
], Skapi.prototype, "logout", null);
|
|
247
|
+
__decorate([
|
|
248
|
+
formHandler({ preventMultipleCalls: true })
|
|
249
|
+
], Skapi.prototype, "signup", null);
|
|
250
|
+
__decorate([
|
|
251
|
+
formHandler({ preventMultipleCalls: true })
|
|
252
|
+
], Skapi.prototype, "resetPassword", null);
|
|
253
|
+
__decorate([
|
|
254
|
+
formHandler({ preventMultipleCalls: true })
|
|
255
|
+
], Skapi.prototype, "verifyEmail", null);
|
|
256
|
+
__decorate([
|
|
257
|
+
formHandler({ preventMultipleCalls: true })
|
|
258
|
+
], Skapi.prototype, "verifyPhoneNumber", null);
|
|
259
|
+
__decorate([
|
|
260
|
+
formHandler({ preventMultipleCalls: true })
|
|
261
|
+
], Skapi.prototype, "forgotPassword", null);
|
|
262
|
+
__decorate([
|
|
263
|
+
formHandler({ preventMultipleCalls: true })
|
|
264
|
+
], Skapi.prototype, "changePassword", null);
|
|
265
|
+
__decorate([
|
|
266
|
+
formHandler({ preventMultipleCalls: true })
|
|
267
|
+
], Skapi.prototype, "updateProfile", null);
|
|
268
|
+
__decorate([
|
|
269
|
+
formHandler()
|
|
270
|
+
], Skapi.prototype, "postRecord", null);
|
|
271
|
+
__decorate([
|
|
272
|
+
formHandler()
|
|
273
|
+
], Skapi.prototype, "subscribe", null);
|
|
274
|
+
__decorate([
|
|
275
|
+
formHandler()
|
|
276
|
+
], Skapi.prototype, "unsubscribe", null);
|
|
277
|
+
__decorate([
|
|
278
|
+
formHandler()
|
|
279
|
+
], Skapi.prototype, "blockSubscriber", null);
|
|
280
|
+
__decorate([
|
|
281
|
+
formHandler()
|
|
282
|
+
], Skapi.prototype, "unblockSubscriber", null);
|
|
283
|
+
__decorate([
|
|
284
|
+
formHandler()
|
|
285
|
+
], Skapi.prototype, "subscribeNewsletter", null);
|