skapi-js 0.2.5 → 1.0.0-alpha.11
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/Types.d.ts +22 -4
- package/js/main/skapi.d.ts +199 -61
- package/js/main/skapi.js +115 -61
- package/js/methods/database.d.ts +13 -18
- package/js/methods/database.js +26 -30
- package/js/methods/request.d.ts +3 -7
- package/js/methods/request.js +37 -50
- package/js/methods/subscription.d.ts +2 -2
- package/js/methods/subscription.js +2 -2
- package/js/methods/user.d.ts +15 -14
- package/js/methods/user.js +46 -11
- package/package.json +5 -7
package/js/methods/database.js
CHANGED
|
@@ -2,7 +2,6 @@ import SkapiError from '../main/error';
|
|
|
2
2
|
import { extractFormMeta, generateRandom } from '../utils/utils';
|
|
3
3
|
import validator from '../utils/validator';
|
|
4
4
|
import { request } from './request';
|
|
5
|
-
import { checkAdmin } from './user';
|
|
6
5
|
const __index_number_range = 4503599627370496;
|
|
7
6
|
function normalizeRecord(record) {
|
|
8
7
|
function base_decode(chars) {
|
|
@@ -149,28 +148,34 @@ function normalizeTypedString(v) {
|
|
|
149
148
|
}
|
|
150
149
|
}
|
|
151
150
|
export async function deleteFiles(params) {
|
|
152
|
-
let
|
|
153
|
-
let { service = this.service, endpoints, storage = 'records' } = params;
|
|
154
|
-
if (storage === 'host' && !isAdmin) {
|
|
155
|
-
throw new SkapiError("No access", { code: 'INVALID_REQUEST' });
|
|
156
|
-
}
|
|
151
|
+
let { endpoints } = params;
|
|
157
152
|
if (typeof endpoints === 'string') {
|
|
158
153
|
endpoints = [endpoints];
|
|
159
154
|
}
|
|
160
155
|
if (!Array.isArray(endpoints)) {
|
|
161
156
|
throw new SkapiError('"endpoints" should be type: array | string.', { code: 'INVALID_PARAMETER' });
|
|
162
157
|
}
|
|
163
|
-
if (storage !== 'host' && storage !== 'records') {
|
|
164
|
-
throw new SkapiError('"storage" should be type: "records" | "host".', { code: 'INVALID_PARAMETER' });
|
|
165
|
-
}
|
|
166
158
|
return request.bind(this)('del-files', {
|
|
167
|
-
service,
|
|
168
159
|
endpoints,
|
|
169
|
-
storage
|
|
160
|
+
storage: 'records'
|
|
170
161
|
}, { auth: true, method: 'post' });
|
|
171
162
|
}
|
|
172
163
|
export async function uploadFiles(fileList, params) {
|
|
173
|
-
|
|
164
|
+
await this.__connection;
|
|
165
|
+
let { service = this.service, request = 'post' } = params || {};
|
|
166
|
+
if (request === 'post') {
|
|
167
|
+
if (!params?.record_id) {
|
|
168
|
+
throw new SkapiError('"record_id" is required.', { code: 'INVALID_PARAMETER' });
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
else {
|
|
172
|
+
if (service === this.service) {
|
|
173
|
+
throw new SkapiError('invalid service.', { code: 'INVALID_PARAMETER' });
|
|
174
|
+
}
|
|
175
|
+
if (request !== 'host') {
|
|
176
|
+
throw new SkapiError('invalid request.', { code: 'INVALID_PARAMETER' });
|
|
177
|
+
}
|
|
178
|
+
}
|
|
174
179
|
if (fileList instanceof SubmitEvent) {
|
|
175
180
|
fileList = fileList.target;
|
|
176
181
|
}
|
|
@@ -193,21 +198,12 @@ export async function uploadFiles(fileList, params) {
|
|
|
193
198
|
let reserved_key = generateRandom();
|
|
194
199
|
let getSignedParams = {
|
|
195
200
|
reserved_key,
|
|
196
|
-
service
|
|
197
|
-
request
|
|
201
|
+
service,
|
|
202
|
+
request
|
|
198
203
|
};
|
|
199
|
-
if (getSignedParams.request === 'host') {
|
|
200
|
-
if (!isAdmin) {
|
|
201
|
-
throw new SkapiError('The user has no access.', { code: 'INVALID_REQUEST' });
|
|
202
|
-
}
|
|
203
|
-
getSignedParams.request === 'post-host';
|
|
204
|
-
}
|
|
205
204
|
if (params?.record_id) {
|
|
206
205
|
getSignedParams.id = params.record_id;
|
|
207
206
|
}
|
|
208
|
-
else if (!isAdmin) {
|
|
209
|
-
throw new SkapiError('Record ID is required.', { code: 'INVALID_PARAMETER' });
|
|
210
|
-
}
|
|
211
207
|
let xhr;
|
|
212
208
|
let fetchProgress = (url, body, progressCallback) => {
|
|
213
209
|
return new Promise((res, rej) => {
|
|
@@ -851,7 +847,7 @@ export async function deleteRecords(params) {
|
|
|
851
847
|
}
|
|
852
848
|
return await request.bind(this)('del-records', params, { auth: true });
|
|
853
849
|
}
|
|
854
|
-
export
|
|
850
|
+
export function grantPrivateRecordAccess(params) {
|
|
855
851
|
if (!params.record_id) {
|
|
856
852
|
throw new SkapiError(`Record ID is required.`, { code: 'INVALID_PARAMETER' });
|
|
857
853
|
}
|
|
@@ -864,7 +860,7 @@ export async function grantPrivateRecordAccess(params) {
|
|
|
864
860
|
execute: 'add'
|
|
865
861
|
});
|
|
866
862
|
}
|
|
867
|
-
export
|
|
863
|
+
export function removePrivateRecordAccess(params) {
|
|
868
864
|
if (!params.record_id) {
|
|
869
865
|
throw new SkapiError(`Record ID is required.`, { code: 'INVALID_PARAMETER' });
|
|
870
866
|
}
|
|
@@ -877,17 +873,17 @@ export async function removePrivateRecordAccess(params) {
|
|
|
877
873
|
execute: 'remove'
|
|
878
874
|
});
|
|
879
875
|
}
|
|
880
|
-
export
|
|
876
|
+
export function listPrivateRecordAccess(params) {
|
|
881
877
|
return recordAccess({
|
|
882
878
|
record_id: params.record_id,
|
|
883
879
|
user_id: params.user_id || null,
|
|
884
880
|
execute: 'list'
|
|
885
881
|
});
|
|
886
882
|
}
|
|
887
|
-
export
|
|
888
|
-
|
|
883
|
+
export function requestPrivateRecordAccessKey(record_id) {
|
|
884
|
+
return request.bind(this)('request-private-access-key', { record_id }, { auth: true });
|
|
889
885
|
}
|
|
890
|
-
|
|
886
|
+
function recordAccess(params) {
|
|
891
887
|
let execute = params.execute;
|
|
892
888
|
let req = validator.Params(params, {
|
|
893
889
|
record_id: 'string',
|
|
@@ -916,5 +912,5 @@ async function recordAccess(params) {
|
|
|
916
912
|
if (!req.user_id) {
|
|
917
913
|
req.user_id = null;
|
|
918
914
|
}
|
|
919
|
-
|
|
915
|
+
return request.bind(this)('grant-private-access', req, { auth: true });
|
|
920
916
|
}
|
package/js/methods/request.d.ts
CHANGED
|
@@ -1,9 +1,5 @@
|
|
|
1
1
|
import { Form, FormSubmitCallback, FetchOptions, Connection } from '../Types';
|
|
2
|
-
export declare function getConnection(): Promise<Connection
|
|
3
|
-
export declare function listHostDirectory(params: {
|
|
4
|
-
service: string;
|
|
5
|
-
dir: string;
|
|
6
|
-
}, fetchOptions: FetchOptions): Promise<any>;
|
|
2
|
+
export declare function getConnection(): Promise<Connection>;
|
|
7
3
|
export declare function request(url: string, data?: Form<any> | null, options?: {
|
|
8
4
|
fetchOptions?: FetchOptions & FormSubmitCallback;
|
|
9
5
|
auth?: boolean;
|
|
@@ -20,14 +16,14 @@ export declare function secureRequest<RequestParams = {
|
|
|
20
16
|
}>(params: RequestParams | RequestParams[]): Promise<any>;
|
|
21
17
|
export declare function mock(data: Form<any | {
|
|
22
18
|
raise?: 'ERR_INVALID_REQUEST' | 'ERR_INVALID_PARAMETER' | 'SOMETHING_WENT_WRONG' | 'ERR_EXISTS' | 'ERR_NOT_EXISTS';
|
|
23
|
-
}>,
|
|
19
|
+
}>, options?: {
|
|
24
20
|
auth?: boolean;
|
|
25
21
|
method?: string;
|
|
26
22
|
meta?: Record<string, any>;
|
|
27
23
|
bypassAwaitConnection?: boolean;
|
|
28
24
|
responseType?: string;
|
|
29
25
|
contentType?: string;
|
|
30
|
-
}): Promise<{
|
|
26
|
+
} & FormSubmitCallback): Promise<{
|
|
31
27
|
mockResponse: Record<string, any>;
|
|
32
28
|
}>;
|
|
33
29
|
export declare function getFormResponse(): Promise<any>;
|
package/js/methods/request.js
CHANGED
|
@@ -5,20 +5,6 @@ const __pendingRequest = {};
|
|
|
5
5
|
export function getConnection() {
|
|
6
6
|
return this.__connection;
|
|
7
7
|
}
|
|
8
|
-
export async function listHostDirectory(params, fetchOptions) {
|
|
9
|
-
let is_admin = await this.checkAdmin();
|
|
10
|
-
if (is_admin) {
|
|
11
|
-
if (!params?.dir) {
|
|
12
|
-
params.dir = '/';
|
|
13
|
-
}
|
|
14
|
-
return request.bind(this)('list-host-directory', params, {
|
|
15
|
-
fetchOptions,
|
|
16
|
-
auth: true,
|
|
17
|
-
method: 'post'
|
|
18
|
-
});
|
|
19
|
-
}
|
|
20
|
-
return [];
|
|
21
|
-
}
|
|
22
8
|
export async function request(url, data = null, options) {
|
|
23
9
|
options = options || {};
|
|
24
10
|
let { auth = false, method = 'post', meta = null, bypassAwaitConnection = false, } = options;
|
|
@@ -225,43 +211,44 @@ export async function request(url, data = null, options) {
|
|
|
225
211
|
if (requestKey && typeof requestKey === 'object') {
|
|
226
212
|
return requestKey;
|
|
227
213
|
}
|
|
228
|
-
if (typeof requestKey
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
else if (!(data instanceof FormData)) {
|
|
245
|
-
headers["Content-Type"] = 'application/json';
|
|
246
|
-
}
|
|
247
|
-
let opt = { headers };
|
|
248
|
-
if (options?.responseType) {
|
|
249
|
-
opt.responseType = options.responseType;
|
|
250
|
-
}
|
|
251
|
-
if (method === 'post') {
|
|
252
|
-
__pendingRequest[requestKey] = _post.bind(this)(endpoint, data, opt, progress);
|
|
253
|
-
}
|
|
254
|
-
else if (method === 'get') {
|
|
255
|
-
__pendingRequest[requestKey] = _get.bind(this)(endpoint, data, opt, progress);
|
|
214
|
+
if (!requestKey || typeof requestKey !== 'string') {
|
|
215
|
+
return null;
|
|
216
|
+
}
|
|
217
|
+
if (!(__pendingRequest[requestKey] instanceof Promise)) {
|
|
218
|
+
let headers = {
|
|
219
|
+
'Accept': '*/*'
|
|
220
|
+
};
|
|
221
|
+
if (token) {
|
|
222
|
+
headers.Authorization = token;
|
|
223
|
+
}
|
|
224
|
+
if (meta) {
|
|
225
|
+
headers["Content-Meta"] = window.btoa(encodeURIComponent(typeof meta === 'string' ? meta : JSON.stringify(meta)));
|
|
226
|
+
}
|
|
227
|
+
if (options.hasOwnProperty('contentType')) {
|
|
228
|
+
if (options?.contentType) {
|
|
229
|
+
headers["Content-Type"] = options.contentType;
|
|
256
230
|
}
|
|
257
231
|
}
|
|
232
|
+
else if (!(data instanceof FormData)) {
|
|
233
|
+
headers["Content-Type"] = 'application/json';
|
|
234
|
+
}
|
|
235
|
+
let opt = { headers };
|
|
236
|
+
if (options?.responseType) {
|
|
237
|
+
opt.responseType = options.responseType;
|
|
238
|
+
}
|
|
239
|
+
if (method === 'post') {
|
|
240
|
+
__pendingRequest[requestKey] = _post.bind(this)(endpoint, data, opt, progress);
|
|
241
|
+
}
|
|
242
|
+
else if (method === 'get') {
|
|
243
|
+
__pendingRequest[requestKey] = _get.bind(this)(endpoint, data, opt, progress);
|
|
244
|
+
}
|
|
258
245
|
try {
|
|
259
246
|
let response = await __pendingRequest[requestKey];
|
|
260
247
|
if (isForm) {
|
|
261
248
|
return response;
|
|
262
249
|
}
|
|
263
250
|
else {
|
|
264
|
-
return
|
|
251
|
+
return update_startKey_keys.bind(this)({
|
|
265
252
|
hashedParam: requestKey,
|
|
266
253
|
url,
|
|
267
254
|
response
|
|
@@ -536,13 +523,13 @@ export async function secureRequest(params) {
|
|
|
536
523
|
return await request.bind(this)('post-secure', params, { auth: true });
|
|
537
524
|
}
|
|
538
525
|
;
|
|
539
|
-
export async function mock(data,
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
526
|
+
export async function mock(data, options) {
|
|
527
|
+
let { auth = true, method = 'POST', meta, bypassAwaitConnection = false, responseType, contentType } = options || {};
|
|
528
|
+
let { response, onerror, formData, progress } = options || {};
|
|
529
|
+
if (options) {
|
|
530
|
+
Object.assign({ auth, method, meta, bypassAwaitConnection, responseType, contentType }, {
|
|
531
|
+
fetchOptions: { response, onerror, formData, progress }
|
|
532
|
+
});
|
|
546
533
|
}
|
|
547
534
|
return request.bind(this)('mock', data, options);
|
|
548
535
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { DatabaseResponse, FetchOptions,
|
|
1
|
+
import { DatabaseResponse, FetchOptions, Form, Newsletters, SubscriptionGroup } from '../Types';
|
|
2
2
|
export declare function getSubscriptions(params: {
|
|
3
3
|
subscriber?: string;
|
|
4
4
|
subscription?: string;
|
|
@@ -33,7 +33,7 @@ export declare function subscribeNewsletter(form: Form<{
|
|
|
33
33
|
email?: string;
|
|
34
34
|
group: number | 'public' | 'authorized';
|
|
35
35
|
redirect?: string;
|
|
36
|
-
}
|
|
36
|
+
}>): Promise<string>;
|
|
37
37
|
export declare function unsubscribeNewsletter(params: {
|
|
38
38
|
group: number | 'public' | 'authorized' | null;
|
|
39
39
|
}): Promise<string>;
|
|
@@ -135,14 +135,14 @@ export async function getNewsletterSubscription(params) {
|
|
|
135
135
|
}
|
|
136
136
|
return result;
|
|
137
137
|
}
|
|
138
|
-
export async function subscribeNewsletter(form
|
|
138
|
+
export async function subscribeNewsletter(form) {
|
|
139
139
|
await this.__connection;
|
|
140
140
|
let params = validator.Params(form || {}, {
|
|
141
141
|
email: (v) => validator.Email(v),
|
|
142
142
|
group: ['number', 'public', 'authorized'],
|
|
143
143
|
redirect: (v) => validator.Url(v)
|
|
144
144
|
}, this.__user ? ['group'] : ['email', 'group']);
|
|
145
|
-
return request.bind(this)(`subscribe-${this.__user ? '' : 'public-'}newsletter`, params, {
|
|
145
|
+
return request.bind(this)(`subscribe-${this.__user ? '' : 'public-'}newsletter`, params, { auth: !!this.__user });
|
|
146
146
|
}
|
|
147
147
|
export async function unsubscribeNewsletter(params) {
|
|
148
148
|
await this.__connection;
|
package/js/methods/user.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { CognitoUser, CognitoUserSession, CognitoUserPool } from 'amazon-cognito-identity-js';
|
|
2
|
-
import {
|
|
2
|
+
import { Form, FormSubmitCallback, UserProfile, FetchOptions, DatabaseResponse, QueryParams, UserAttributes, PublicUser } from '../Types';
|
|
3
3
|
export declare let userPool: CognitoUserPool | null;
|
|
4
4
|
export declare function setUserPool(params: {
|
|
5
5
|
UserPoolId: string;
|
|
@@ -9,7 +9,7 @@ export declare function authentication(): {
|
|
|
9
9
|
getSession: (option?: {
|
|
10
10
|
refreshToken?: boolean;
|
|
11
11
|
}) => Promise<CognitoUserSession>;
|
|
12
|
-
authenticateUser: (email: string, password: string) => Promise<
|
|
12
|
+
authenticateUser: (email: string, password: string) => Promise<UserProfile>;
|
|
13
13
|
createCognitoUser: (email: string) => Promise<{
|
|
14
14
|
cognitoUser: CognitoUser;
|
|
15
15
|
cognitoUsername: string;
|
|
@@ -18,15 +18,16 @@ export declare function authentication(): {
|
|
|
18
18
|
};
|
|
19
19
|
export declare function getProfile(options?: {
|
|
20
20
|
refreshToken: boolean;
|
|
21
|
-
}): Promise<
|
|
21
|
+
}): Promise<UserProfile | null>;
|
|
22
22
|
export declare function checkAdmin(): Promise<boolean>;
|
|
23
|
-
export declare function logout(
|
|
23
|
+
export declare function logout(): Promise<'SUCCESS: The user has been logged out.'>;
|
|
24
24
|
export declare function resendSignupConfirmation(redirect: string): Promise<'SUCCESS: Signup confirmation E-Mail has been sent.'>;
|
|
25
25
|
export declare function recoverAccount(redirect?: boolean | string): Promise<"SUCCESS: Recovery e-mail has been sent.">;
|
|
26
26
|
export declare function login(form: Form<{
|
|
27
|
+
username: string;
|
|
27
28
|
email: string;
|
|
28
29
|
password: string;
|
|
29
|
-
}
|
|
30
|
+
}>): Promise<UserProfile>;
|
|
30
31
|
export declare function signup(form: Form<UserAttributes & {
|
|
31
32
|
email: String;
|
|
32
33
|
password: String;
|
|
@@ -34,32 +35,32 @@ export declare function signup(form: Form<UserAttributes & {
|
|
|
34
35
|
signup_confirmation?: boolean | string;
|
|
35
36
|
email_subscription?: boolean;
|
|
36
37
|
login?: boolean;
|
|
37
|
-
} & FormSubmitCallback): Promise<
|
|
38
|
+
} & FormSubmitCallback): Promise<UserProfile | "SUCCESS: The account has been created. User's signup confirmation is required." | 'SUCCESS: The account has been created.'>;
|
|
38
39
|
export declare function disableAccount(): Promise<'SUCCESS: account has been disabled.'>;
|
|
39
40
|
export declare function resetPassword(form: Form<{
|
|
40
41
|
email: string;
|
|
41
42
|
code: string | number;
|
|
42
43
|
new_password: string;
|
|
43
|
-
}
|
|
44
|
-
export declare function verifyPhoneNumber(form
|
|
44
|
+
}>): Promise<"SUCCESS: New password has been set.">;
|
|
45
|
+
export declare function verifyPhoneNumber(form?: Form<{
|
|
45
46
|
code: string;
|
|
46
47
|
}>): Promise<'SUCCESS: Verification code has been sent.' | 'SUCCESS: "phone_number" is verified.'>;
|
|
47
|
-
export declare function verifyEmail(form
|
|
48
|
+
export declare function verifyEmail(form?: Form<{
|
|
48
49
|
code: string;
|
|
49
50
|
}>): Promise<'SUCCESS: Verification code has been sent.' | 'SUCCESS: "email" is verified.'>;
|
|
50
51
|
export declare function forgotPassword(form: Form<{
|
|
51
52
|
email: string;
|
|
52
|
-
}
|
|
53
|
+
}>): Promise<"SUCCESS: Verification code has been sent.">;
|
|
53
54
|
export declare function changePassword(params: {
|
|
54
55
|
new_password: string;
|
|
55
56
|
current_password: string;
|
|
56
|
-
}): Promise<
|
|
57
|
-
export declare function updateProfile(form: Form<UserAttributes
|
|
58
|
-
export declare function getUsers(params?: QueryParams | null, fetchOptions?: FetchOptions): Promise<DatabaseResponse<
|
|
57
|
+
}): Promise<'SUCCESS: Password has been changed.'>;
|
|
58
|
+
export declare function updateProfile(form: Form<UserAttributes>): Promise<UserProfile>;
|
|
59
|
+
export declare function getUsers(params?: QueryParams | null, fetchOptions?: FetchOptions): Promise<DatabaseResponse<PublicUser>>;
|
|
59
60
|
export declare function lastVerifiedEmail(params?: {
|
|
60
61
|
revert: boolean;
|
|
61
62
|
}): Promise<string | UserProfile>;
|
|
62
63
|
export declare function requestUsernameChange(params: {
|
|
63
64
|
redirect?: string;
|
|
64
65
|
username: string;
|
|
65
|
-
}): Promise<'SUCCESS:
|
|
66
|
+
}): Promise<'SUCCESS: confirmation e-mail has been sent.'>;
|
package/js/methods/user.js
CHANGED
|
@@ -215,7 +215,19 @@ export function authentication() {
|
|
|
215
215
|
cognitoUser.authenticateUser(authenticationDetails, {
|
|
216
216
|
newPasswordRequired: (userAttributes, requiredAttributes) => {
|
|
217
217
|
this.__request_signup_confirmation = username;
|
|
218
|
-
|
|
218
|
+
if (userAttributes['custom:signup_ticket'] === 'PASS' || userAttributes['custom:signup_ticket'] === 'MEMBER') {
|
|
219
|
+
cognitoUser.completeNewPasswordChallenge(password, {}, {
|
|
220
|
+
onSuccess: (result) => {
|
|
221
|
+
getSession().then(session => res(this.user));
|
|
222
|
+
},
|
|
223
|
+
onFailure: (err) => {
|
|
224
|
+
rej(new SkapiError(err.message || 'Failed to authenticate user.', { code: err.code }));
|
|
225
|
+
}
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
else {
|
|
229
|
+
rej(new SkapiError("User's signup confirmation is required.", { code: 'SIGNUP_CONFIRMATION_NEEDED' }));
|
|
230
|
+
}
|
|
219
231
|
},
|
|
220
232
|
onSuccess: (logged) => getSession().then(session => res(this.user)),
|
|
221
233
|
onFailure: (err) => {
|
|
@@ -243,7 +255,12 @@ export function authentication() {
|
|
|
243
255
|
});
|
|
244
256
|
});
|
|
245
257
|
};
|
|
246
|
-
return {
|
|
258
|
+
return {
|
|
259
|
+
getSession,
|
|
260
|
+
authenticateUser,
|
|
261
|
+
createCognitoUser,
|
|
262
|
+
getUser,
|
|
263
|
+
};
|
|
247
264
|
}
|
|
248
265
|
export async function getProfile(options) {
|
|
249
266
|
await this.__connection;
|
|
@@ -265,7 +282,7 @@ export async function checkAdmin() {
|
|
|
265
282
|
}
|
|
266
283
|
return false;
|
|
267
284
|
}
|
|
268
|
-
export async function logout(
|
|
285
|
+
export async function logout() {
|
|
269
286
|
await this.__connection;
|
|
270
287
|
if (cognitoUser) {
|
|
271
288
|
cognitoUser.signOut();
|
|
@@ -311,17 +328,18 @@ export async function recoverAccount(redirect = false) {
|
|
|
311
328
|
this.__disabledAccount = null;
|
|
312
329
|
return 'SUCCESS: Recovery e-mail has been sent.';
|
|
313
330
|
}
|
|
314
|
-
export async function login(form
|
|
331
|
+
export async function login(form) {
|
|
315
332
|
await logout.bind(this)();
|
|
316
333
|
let params = validator.Params(form, {
|
|
334
|
+
username: 'string',
|
|
317
335
|
email: (v) => validator.Email(v),
|
|
318
336
|
password: (v) => validator.Password(v)
|
|
319
337
|
}, ['email', 'password']);
|
|
320
|
-
return authentication.bind(this)().authenticateUser(params.email, params.password);
|
|
338
|
+
return authentication.bind(this)().authenticateUser(params.username || params.email, params.password);
|
|
321
339
|
}
|
|
322
340
|
export async function signup(form, option) {
|
|
323
|
-
await this.logout();
|
|
324
341
|
let params = validator.Params(form || {}, {
|
|
342
|
+
username: 'string',
|
|
325
343
|
email: (v) => validator.Email(v),
|
|
326
344
|
password: (v) => validator.Password(v),
|
|
327
345
|
name: 'string',
|
|
@@ -334,8 +352,20 @@ export async function signup(form, option) {
|
|
|
334
352
|
gender_public: ['boolean', () => false],
|
|
335
353
|
birthdate_public: ['boolean', () => false],
|
|
336
354
|
phone_number_public: ['boolean', () => false],
|
|
355
|
+
access_group: 'number',
|
|
337
356
|
misc: 'string'
|
|
338
357
|
}, ['email', 'password']);
|
|
358
|
+
let is_admin = await checkAdmin.bind(this)();
|
|
359
|
+
let admin_creating_account = is_admin && params.service && this.service !== params.service;
|
|
360
|
+
if (admin_creating_account) {
|
|
361
|
+
params.owner = this.__user.user_id;
|
|
362
|
+
}
|
|
363
|
+
else {
|
|
364
|
+
if (params.access_group) {
|
|
365
|
+
throw new SkapiError('Only admins can set "access_group" parameter.', { code: 'INVALID_PARAMETER' });
|
|
366
|
+
}
|
|
367
|
+
await this.logout();
|
|
368
|
+
}
|
|
339
369
|
option = validator.Params(option || {}, {
|
|
340
370
|
email_subscription: (v) => {
|
|
341
371
|
if (typeof v !== 'boolean') {
|
|
@@ -369,20 +399,25 @@ export async function signup(form, option) {
|
|
|
369
399
|
});
|
|
370
400
|
let logUser = option?.login || false;
|
|
371
401
|
let signup_confirmation = option?.signup_confirmation || false;
|
|
402
|
+
if (admin_creating_account && signup_confirmation) {
|
|
403
|
+
throw new SkapiError('Admins cannot create an account with "option.signup_confirmation" option.', { code: 'INVALID_PARAMETER' });
|
|
404
|
+
}
|
|
372
405
|
if (params.email_public && !signup_confirmation) {
|
|
373
406
|
throw new SkapiError('"option.signup_confirmation" should be true if "email_public" is set to true.', { code: 'INVALID_PARAMETER' });
|
|
374
407
|
}
|
|
375
408
|
params.signup_confirmation = signup_confirmation;
|
|
376
409
|
params.email_subscription = option?.email_subscription || false;
|
|
410
|
+
delete params.service;
|
|
411
|
+
delete params.owner;
|
|
377
412
|
await request.bind(this)("signup", params);
|
|
378
413
|
if (signup_confirmation) {
|
|
379
|
-
let u = await authentication.bind(this)().createCognitoUser(params.email);
|
|
414
|
+
let u = await authentication.bind(this)().createCognitoUser(params.username || params.email);
|
|
380
415
|
cognitoUser = u.cognitoUser;
|
|
381
416
|
this.__request_signup_confirmation = u.cognitoUsername;
|
|
382
417
|
return "SUCCESS: The account has been created. User's signup confirmation is required.";
|
|
383
418
|
}
|
|
384
419
|
if (logUser) {
|
|
385
|
-
return login.bind(this)({ email: params.email, password: params.password });
|
|
420
|
+
return login.bind(this)({ email: params.username || params.email, password: params.password });
|
|
386
421
|
}
|
|
387
422
|
return 'SUCCESS: The account has been created.';
|
|
388
423
|
}
|
|
@@ -392,7 +427,7 @@ export async function disableAccount() {
|
|
|
392
427
|
await logout.bind(this)();
|
|
393
428
|
return result;
|
|
394
429
|
}
|
|
395
|
-
export async function resetPassword(form
|
|
430
|
+
export async function resetPassword(form) {
|
|
396
431
|
await this.__connection;
|
|
397
432
|
let params = validator.Params(form, {
|
|
398
433
|
email: (v) => validator.Email(v),
|
|
@@ -473,7 +508,7 @@ export function verifyPhoneNumber(form) {
|
|
|
473
508
|
export function verifyEmail(form) {
|
|
474
509
|
return verifyAttribute.bind(this)('email', form);
|
|
475
510
|
}
|
|
476
|
-
export async function forgotPassword(form
|
|
511
|
+
export async function forgotPassword(form) {
|
|
477
512
|
await this.__connection;
|
|
478
513
|
let params = validator.Params(form, {
|
|
479
514
|
email: (v) => validator.Email(v)
|
|
@@ -524,7 +559,7 @@ export async function changePassword(params) {
|
|
|
524
559
|
});
|
|
525
560
|
});
|
|
526
561
|
}
|
|
527
|
-
export async function updateProfile(form
|
|
562
|
+
export async function updateProfile(form) {
|
|
528
563
|
await this.__connection;
|
|
529
564
|
if (!this.session) {
|
|
530
565
|
throw new SkapiError('User login is required.', { code: 'INVALID_REQUEST' });
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "skapi-js",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.0-alpha.11",
|
|
4
4
|
"description": "Javascript library for Skapi: Complete JAM Stack, front-end driven serverless backend API service.",
|
|
5
5
|
"main": "./dist/skapi.module.js",
|
|
6
6
|
"types": "./js/Main.d.ts",
|
|
@@ -9,8 +9,8 @@
|
|
|
9
9
|
"dist/"
|
|
10
10
|
],
|
|
11
11
|
"scripts": {
|
|
12
|
-
"build": "npx tsc --project tsconfig.json; npx webpack --config webpack.config.js",
|
|
13
|
-
"
|
|
12
|
+
"build": "npx tsc --project tsconfig.json; npx webpack --config webpack.config.js; cp -r dist playground;",
|
|
13
|
+
"dev": "(cd playground; node server.js)"
|
|
14
14
|
},
|
|
15
15
|
"repository": {
|
|
16
16
|
"type": "git",
|
|
@@ -24,6 +24,7 @@
|
|
|
24
24
|
},
|
|
25
25
|
"homepage": "https://www.skapi.com",
|
|
26
26
|
"devDependencies": {
|
|
27
|
+
"amazon-cognito-identity-js": "^5.2.14",
|
|
27
28
|
"typescript": "^4.9.4",
|
|
28
29
|
"webpack": "^5.74.0",
|
|
29
30
|
"webpack-cli": "^4.10.0"
|
|
@@ -33,8 +34,5 @@
|
|
|
33
34
|
"skapi",
|
|
34
35
|
"api",
|
|
35
36
|
"jamstack"
|
|
36
|
-
]
|
|
37
|
-
"dependencies": {
|
|
38
|
-
"amazon-cognito-identity-js": "^5.2.14"
|
|
39
|
-
}
|
|
37
|
+
]
|
|
40
38
|
}
|