skapi-js 0.0.48 → 0.0.49

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/deploy.py CHANGED
@@ -2,15 +2,20 @@ import json
2
2
  import os
3
3
 
4
4
  ret = os.system('git pull')
5
-
5
+
6
6
  if ret != 0:
7
7
  print('==Failed to pull==')
8
8
 
9
+ ret = os.system('npm run build')
10
+
11
+ if ret != 0:
12
+ print('==Failed to build==')
13
+
9
14
  with open('./package.json', 'r') as p:
10
15
  package = json.loads(p.read())
11
16
 
12
17
  ret = os.system('npm publish')
13
-
18
+
14
19
  if ret != 0:
15
20
  print('==Failed to publish==')
16
21
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "skapi-js",
3
- "version": "0.0.48",
3
+ "version": "0.0.49",
4
4
  "description": "skapi serverless cloud javascript library",
5
5
  "main": "./dist/skapi.module.js",
6
6
  "scripts": {
@@ -18,8 +18,8 @@
18
18
  },
19
19
  "homepage": "https://skapi.com",
20
20
  "devDependencies": {
21
- "typedoc": "^0.23.18",
22
- "typescript": "^4.8.4",
21
+ "typedoc": "^0.23.21",
22
+ "typescript": "^4.9.4",
23
23
  "webpack": "^5.74.0",
24
24
  "webpack-cli": "^4.10.0"
25
25
  },
@@ -31,6 +31,7 @@
31
31
  "cloud"
32
32
  ],
33
33
  "dependencies": {
34
- "amazon-cognito-identity-js": "^5.2.12"
34
+ "amazon-cognito-identity-js": "^5.2.12",
35
+ "build": "^0.1.4"
35
36
  }
36
- }
37
+ }
package/src/Types.ts CHANGED
@@ -278,9 +278,7 @@ export type RecordData = {
278
278
  /** Allows multiple reference if true. */
279
279
  allow_multi_reference: boolean;
280
280
  /** Allows referencing if true. */
281
- reference_limit?: number;
282
- /** List of user id that has private access */
283
- private_access?: string[];
281
+ reference_limit: number;
284
282
  };
285
283
  /** Record id that the record is referencing. */
286
284
  reference?: string;
package/src/skapi.ts CHANGED
@@ -97,7 +97,7 @@ export default class Skapi {
97
97
  private __cached_requests: CachedRequests = {};
98
98
  private __startKey_keys: StartKeys = {};
99
99
  private __request_signup_confirmation: string | null = null;
100
- private __index_number_range = 4503599627370496;
100
+ private __index_number_range = 4503599627370496; // +/-
101
101
  private service: string;
102
102
  private service_owner: string;
103
103
 
@@ -176,9 +176,21 @@ export default class Skapi {
176
176
  }
177
177
  };
178
178
 
179
- /** @ignore */
180
179
  __connection: Promise<Connection | null>;
181
180
 
181
+ /**
182
+ * Awaits connection to be established.
183
+ * You can use this to make sure the connection is established.
184
+ *
185
+ * ```
186
+ * let skapi = new Skapi();
187
+ * skapi.awaitConnection().then(connection=>console.log('Connected to Server'));
188
+ * ```
189
+ */
190
+ async awaitConnection(): Promise<Connection | null> {
191
+ return this.__connection;
192
+ }
193
+
182
194
  // skapi int range -4503599627370545 ~ 4503599627370546
183
195
 
184
196
  constructor(service_id: string, service_owner: string, autoLogin: boolean = false) {
@@ -288,50 +300,6 @@ export default class Skapi {
288
300
  })(this).catch(err => { throw err; });
289
301
  }
290
302
 
291
- /**
292
- * Connects to your Skapi web service.</br>
293
- * Use your service ID and your user ID as an argument.<br>
294
- * Once successful, Skapi class object will be returned.<br>
295
- *
296
- * <h5>IMPORTANT NOTE!</h5>
297
- * When setting up your web services in Skapi, always set your service cors url on production.<br>
298
- * If the cors is not set, other people can use your web service apis on their websites as well.<br>
299
- * Refer: <a href='www.google.com'>Setting up cors</a>
300
- *
301
- * ```
302
- * Skapi.connect('xxxxxxxxxxxxxx', 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx').then(async skapi => {
303
- *
304
- * console.log(skapi.connection); // connection to Skapi!
305
- *
306
- * if(skapi.user) {
307
- * // user is logged in!
308
- * }
309
- *
310
- * else {
311
- * // user is not logged in.
312
- * }
313
- *
314
- * // - code -
315
- *
316
- * }).catch(error => {
317
- * console.log('Connection error'); //'connection failed.'
318
- * });
319
- * ```
320
- * @category Connection
321
- */
322
- static async connect(
323
- /** service ID */
324
- service_id: string,
325
- /** service owner's user ID */
326
- service_owner: string,
327
- /** Auto login user when true */
328
- autoLogin: boolean = false
329
- ): Promise<Skapi> {
330
- const skapi = new Skapi(service_id, service_owner, autoLogin);
331
- await skapi.__connection;
332
- return skapi;
333
- }
334
-
335
303
  private authentication() {
336
304
  if (!this.userPool) {
337
305
  throw new SkapiError('User pool is missing', { code: 'INVALID_REQUEST' });
@@ -540,31 +508,16 @@ export default class Skapi {
540
508
  return { updateSession, authenticateUser, createCognitoUser };
541
509
  }
542
510
 
543
- /**
544
- * @ignore
545
- */
546
- async requireAdmin(option?: { ignoreVerification?: boolean; throwError?: boolean; }) {
511
+ async checkAdmin() {
512
+ await this.__connection;
513
+
547
514
  if (this.session?.attributes?.['custom:service'] === this.service) {
548
515
  // logged in
549
- if (this.session?.attributes?.['custom:service_owner'] === this.host) {
550
- if (!option?.ignoreVerification && !this.session?.attributes?.email_verified) {
551
- throw new SkapiError('Email verification is required.', { code: 'INVALID_REQUEST' });
552
- }
553
-
554
- return true;
555
- }
556
-
557
- if (option?.throwError) {
558
- throw new SkapiError('Admin access is required.', { code: 'INVALID_REQUEST' });
559
- }
516
+ return this.session?.attributes?.['custom:service_owner'] === this.host;
560
517
 
561
518
  } else {
562
519
  // not logged
563
520
  this.logout();
564
-
565
- if (option?.throwError) {
566
- throw new SkapiError('User login is required.', { code: 'INVALID_REQUEST' });
567
- }
568
521
  }
569
522
 
570
523
  return false;
@@ -591,7 +544,21 @@ export default class Skapi {
591
544
  return await this.request(p.url, option || null, { method: 'get', auth: p.url.includes('/auth/'), contentType: null, responseType: 'blob' });
592
545
  }
593
546
 
594
- async mock(data, options) {
547
+ /**
548
+ * mock skapi api
549
+ * @param data data to send
550
+ * @param options fetch options
551
+ * @returns mock response
552
+ */
553
+ async mock(data: any, options?: {
554
+ fetchOptions?: FetchOptions & FormCallbacks;
555
+ auth?: boolean;
556
+ method?: string;
557
+ meta?: Record<string, any>;
558
+ bypassAwaitConnection?: boolean;
559
+ responseType?: string;
560
+ contentType?: string;
561
+ }): Promise<{ mockResponse: Record<string, any>; }> {
595
562
  return this.request('test-api', data, options);
596
563
  }
597
564
 
@@ -790,13 +757,10 @@ export default class Skapi {
790
757
 
791
758
  // internals below
792
759
 
793
- /**
794
- * @ignore
795
- */
796
760
  async request(
797
761
  url: string,
798
762
  data: Form = null,
799
- options: {
763
+ options?: {
800
764
  fetchOptions?: FetchOptions & FormCallbacks;
801
765
  auth?: boolean;
802
766
  method?: string;
@@ -804,14 +768,14 @@ export default class Skapi {
804
768
  bypassAwaitConnection?: boolean;
805
769
  responseType?: string;
806
770
  contentType?: string;
807
- } = {}): Promise<any> {
771
+ }): Promise<any> {
808
772
 
809
773
  let {
810
774
  auth = false,
811
775
  method = 'post',
812
776
  meta = null, // content meta
813
777
  bypassAwaitConnection = false
814
- } = options;
778
+ } = options || {};
815
779
 
816
780
  let __connection = bypassAwaitConnection ? null : (await this.__connection);
817
781
  let token = auth ? this.session?.idToken?.jwtToken : null; // idToken
@@ -854,6 +818,7 @@ export default class Skapi {
854
818
  case 'confirm-signup':
855
819
  case 'recover-account':
856
820
  case 'test-api':
821
+ case 'get-services':
857
822
  case 'service':
858
823
  return {
859
824
  public: admin.admin_public,
@@ -1433,7 +1398,7 @@ export default class Skapi {
1433
1398
  option: PostRecordParams & FormCallbacks
1434
1399
  ): Promise<RecordData> {
1435
1400
 
1436
- let is_admin = await this.requireAdmin({ ignoreVerification: true });
1401
+ let isAdmin = await this.checkAdmin();
1437
1402
  if (!option) {
1438
1403
  throw new SkapiError(['INVALID_PARAMETER', '"option" argument is required.']);
1439
1404
  }
@@ -1559,7 +1524,7 @@ export default class Skapi {
1559
1524
  }
1560
1525
  }
1561
1526
 
1562
- if (is_admin) {
1527
+ if (isAdmin) {
1563
1528
  if (option?.access_group === 'private') {
1564
1529
  throw new SkapiError('Service owner cannot write private records.', { code: 'INVALID_REQUEST' });
1565
1530
  }
@@ -2014,7 +1979,7 @@ export default class Skapi {
2014
1979
  subscription_group?: number;
2015
1980
  };
2016
1981
  }): Promise<string> {
2017
- let isAdmin = await this.requireAdmin();
1982
+ let isAdmin = await this.checkAdmin();
2018
1983
  if (isAdmin && !params?.service) {
2019
1984
  throw new SkapiError('Service ID is required.', { code: 'INVALID_PARAMETER' });
2020
1985
  }
@@ -2400,7 +2365,7 @@ export default class Skapi {
2400
2365
  },
2401
2366
  fetchOptions?: FetchOptions
2402
2367
  ): Promise<Newsletters> {
2403
- let isAdmin = await this.requireAdmin();
2368
+ let isAdmin = await this.checkAdmin();
2404
2369
 
2405
2370
  let searchType = {
2406
2371
  'message_id': 'string',
@@ -3156,57 +3121,6 @@ export default class Skapi {
3156
3121
  return this.__user;
3157
3122
  }
3158
3123
 
3159
-
3160
- /**
3161
- * Uploads user's account profile data.<br>
3162
- * The account needs to be signed in.
3163
- *
3164
- * ```
3165
- * await skapi.updateUserData(
3166
- * {
3167
- * whoAmI: 'John Lennon',
3168
- * myFavoriteFruits: ['Strawberry', 'Apple'],
3169
- * meAndYoko: {
3170
- * pictures: ['traveling.jpg', 'in_the_studio.jpg'],
3171
- * unreleased_videos: ['give_a_peace_a_chance.mpg']
3172
- * },
3173
- * secret: 'Walrus was paul'
3174
- * },
3175
- * {
3176
- * private: ['meAndYoko', 'secret']
3177
- * }
3178
- * );
3179
- *
3180
- * // userdata.meAndYoko, userdata.secret will not be public
3181
- * ```
3182
- * @category User
3183
- */
3184
- @formResponse()
3185
- async uploadUserData(
3186
- /** Form element or object. */
3187
- form: Form,
3188
- option?: FormCallbacks & {
3189
- /** Lists of key to make private. */
3190
- private: string[];
3191
- }): Promise<User> {
3192
-
3193
- option = checkParams(option || {}, {
3194
- private: 'array'
3195
- });
3196
-
3197
- let opt = {
3198
- auth: true
3199
- };
3200
-
3201
- if (option?.private) {
3202
- Object.assign(opt, { meta: { '__private__': option.private } });
3203
- }
3204
-
3205
- await this.request('post-userdata', form, opt);
3206
- await this.authentication().updateSession();
3207
- return this.__user;
3208
- }
3209
-
3210
3124
  /**
3211
3125
  * Query and fetch user account database.<br>
3212
3126
  * Any attribute that user has set to private will not be searchable.<br>
@@ -3216,26 +3130,7 @@ export default class Skapi {
3216
3130
  * You can search for user's information based on:<br>
3217
3131
  * 'user_id' | 'email' | 'phone_number' | 'name' | 'address' | 'group' | 'email_subscription' | 'gender' | 'birthdate' | 'locale' | 'subscribers'</br>
3218
3132
  * User's will not be on the database if they did not login after signup.
3219
- *
3220
- * ```
3221
- * let getPauls = await skapi.getUsers({
3222
- * searchFor: 'name',
3223
- * value: 'paul', // search all user profile names in the connection service that is/start's with 'paul'
3224
- * condition: '>='
3225
- * });
3226
- *
3227
- * let getAllUsers = await skapi.getUsers({
3228
- * searchFor: 'group',
3229
- * value: 1 // search all users in group 1
3230
- * });
3231
- *
3232
- * // returns -
3233
- * // {
3234
- * // list: [<object | user objects>, ...],
3235
- * // startKey: <object>,
3236
- * // endOfList: false // if true, more list can be fetched when method is executed again.
3237
- * // }
3238
- * ```
3133
+ *
3239
3134
  *
3240
3135
  * form.searchFor: Index to search.
3241
3136
  * Search for user's information based on:<br>
@@ -3300,14 +3195,9 @@ export default class Skapi {
3300
3195
  *
3301
3196
  * @category User
3302
3197
  */
3303
- async getUsers(
3304
- params?: QueryParams | null,
3305
- fetchOptions?: FetchOptions
3306
- ): Promise<User | FetchResponse> {
3198
+ async getUsers(params?: QueryParams | null, fetchOptions?: FetchOptions): Promise<User | FetchResponse> {
3307
3199
 
3308
- let isAdmin = await this.requireAdmin({
3309
- ignoreVerification: true
3310
- });
3200
+ let isAdmin = await this.checkAdmin();
3311
3201
 
3312
3202
  if (!params) {
3313
3203
  if (!fetchOptions) {
@@ -3424,51 +3314,19 @@ export default class Skapi {
3424
3314
 
3425
3315
  let isSelfProfile = params.searchFor === 'user_id' && params.value === this.session.idToken.payload.sub;
3426
3316
 
3427
- if (isAdmin && !isSelfProfile && !(params as any).service) {
3317
+ if (isSelfProfile) {
3318
+ return JSON.parse(JSON.stringify(this.__user));
3319
+ }
3320
+
3321
+ if (isAdmin && !params.hasOwnProperty('service')) {
3428
3322
  throw new SkapiError('Service ID is required.', { code: 'INVALID_PARAMETER' });
3429
3323
  }
3430
3324
 
3431
- let result = await this.request(
3325
+ return this.request(
3432
3326
  'get-users',
3433
3327
  params,
3434
3328
  { auth: true, fetchOptions }
3435
3329
  );
3436
-
3437
- if (!result.list[0] || !isSelfProfile || result.list.length > 1) {
3438
- return result;
3439
- }
3440
-
3441
- let user = result.list[0];
3442
- // append user session data
3443
- Object.assign(user, this.__user);
3444
-
3445
- user._what_public_see = JSON.parse(JSON.stringify(user));
3446
-
3447
- let public_keys = [
3448
- 'service',
3449
- 'user_id',
3450
- 'name',
3451
- 'locale',
3452
- 'address',
3453
- 'birthdate',
3454
- 'phone_number',
3455
- 'email',
3456
- 'gender',
3457
- 'subscribers',
3458
- 'timestamp',
3459
- 'group',
3460
- 'log',
3461
- 'user_data'
3462
- ];
3463
-
3464
- // remove unnecessary keys in _what_public_see
3465
- for (let k in user._what_public_see) {
3466
- if (!public_keys.includes(k)) {
3467
- delete user._what_public_see[k];
3468
- }
3469
- }
3470
-
3471
- return user;
3472
3330
  }
3473
3331
 
3474
3332
  private async updateConnection(params?: { request_hash: string; }): Promise<Connection> {