skapi-js 0.0.1 → 0.0.3

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "skapi-js",
3
- "version": "0.0.1",
3
+ "version": "0.0.3",
4
4
  "description": "skapi javascript library",
5
5
  "main": "./dist/skapi.js",
6
6
  "scripts": {
package/src/Types.ts CHANGED
@@ -1,3 +1,20 @@
1
+ export type Connection = {
2
+ /** Connection locale */
3
+ locale: string;
4
+ /** User ID of the service owner */
5
+ owner: string;
6
+ /** E-Mail address of the service owner */
7
+ email: string;
8
+ /** Service id */
9
+ service: string;
10
+ /** Service region */
11
+ region: string;
12
+ /** 13 digits timestamp of the service creation */
13
+ timestamp: number;
14
+ /** Hash string for authentication */
15
+ hash: string;
16
+ };
17
+
1
18
  /**
2
19
  * Additional option for form requests.
3
20
  * You can attach callbacks on response and error.
@@ -11,7 +28,7 @@
11
28
  * ```
12
29
  */
13
30
  export type FormCallbacks = {
14
- /** Callback for response */
31
+ /** Callback for form response */
15
32
  response?(response: any): any;
16
33
  /** Callback on error. When boolen true is given, alertbox will show. */
17
34
  onerror?: (error: Error) => any;
@@ -59,6 +76,16 @@ export type Newsletters = {
59
76
  };
60
77
 
61
78
  export type UserProfile = {
79
+ /** Service id of the user account. */
80
+ service: string;
81
+ /** User ID of the service owner. */
82
+ service_owner?: string;
83
+ /** Access level of the user's account. */
84
+ access_group?: number;
85
+ /** User's ID. */
86
+ user_id: string;
87
+ /** Country code of where user signed up from. */
88
+ locale: string;
62
89
  /**
63
90
  * User's E-Mail for signin.<br>
64
91
  * 64 character max.<br>
@@ -67,6 +94,18 @@ export type UserProfile = {
67
94
  * E-Mail should be verified to set to public.
68
95
  * */
69
96
  email?: string;
97
+ /** Shows true when user has verified their E-Mail. */
98
+ email_verified?: boolean;
99
+ /**
100
+ * User's phone number. Format: "+0012341234"<br>
101
+ * When phone number is changed, phone number verified state will be changed to false.<br>
102
+ * Phone number is only visible to others when set to public.<br>
103
+ * Phone number should be verified to set to public.
104
+ */
105
+ phone_number?: string;
106
+ /** Shows true when user has verified their phone number. */
107
+ phone_number_verified?: boolean;
108
+
70
109
  /** User's name */
71
110
  name?: string;
72
111
  /** User's address */
@@ -78,62 +117,37 @@ export type UserProfile = {
78
117
  gender?: string;
79
118
  /** User's birthdate. String format: "1969-07-16" */
80
119
  birthdate?: string;
81
- /**
82
- * User's phone number. Format: "+0012341234"<br>
83
- * When phone number is changed, phone number verified state will be changed to false.<br>
84
- * Phone number is only visible to others when set to public.<br>
85
- * Phone number should be verified to set to public.
86
- */
87
- phone_number?: string;
88
- /** Subscribes to service newsletters. E-Mail should be verified. */
89
- email_subscription?: boolean;
90
- /** When true, Set's E-Mail to public. E-Mail should be verified. */
120
+ /** User has subscribed to service e-mail when positive number. E-mail should be verified. */
121
+ email_subscription?: number;
122
+ /** User's E-mail is public when positive number. E-Mail should be verified. */
91
123
  email_public?: boolean;
92
- /** When true, Set's phone number to public. Phone number should be verified. */
124
+ /** User's phone number is public when positive number. Phone number should be verified. */
93
125
  phone_number_public?: boolean;
94
- /** When true, Set's address to public. */
126
+ /** User's address is public when positive number. */
95
127
  address_public?: boolean;
96
- /** When true, Set's gender to public. */
128
+ /** User's gender is public when positive number. */
97
129
  gender_public?: boolean;
98
- /** When true, Set's birthdate to public. */
130
+ /** User's birthdate is public when positive number. */
99
131
  birthdate_public?: boolean;
132
+ /** Shows 'PASS' if the user's account signup was successful. */
133
+ signup_ticket?: string;
100
134
  };
101
135
 
102
136
  export interface User extends UserProfile {
103
- /** Service id of the user account. */
104
- service: string;
105
- /** User id of the service owner. */
106
- service_owner?: string;
107
- /** Shows true when user has verified their E-Mail. */
108
- email_verified?: boolean;
109
- /** User group that user account is in. */
110
- group?: number;
111
- /** Country code of where user signed up from. */
112
- locale: string;
113
137
  /** Last login time */
114
138
  log: number;
115
- /** Shows true when user has verified their phone number. */
116
- phone_number_verified?: boolean;
117
139
  /** User data that has been set to private. The data is only shown to the owner of the account. */
118
140
  private_data?: Record<string, any>;
119
- /** Shows 'PASS' if the account signup was successful. */
120
- signup_ticket?: string;
121
- /** User id. */
122
- sub: string;
123
- /** Number of the users subscribers. */
141
+ /** Number of the user's subscribers. */
124
142
  subscribers: number;
125
143
  /** Timestamp of user signup time. */
126
144
  timestamp: number;
127
- /** User data. */
145
+ /** User's data. */
128
146
  user_data?: Record<string, any>;
129
- /** User id. Same as 'sub'. */
130
- user_id: string;
131
147
  /** Reference of how others would see the data. Appears only on the owner of the account. */
132
- what_public_see?: Record<string, any>;
133
- /** Service that account owns. Appears only on admin users. */
134
- services?: Array<Service>;
135
- /** Service access group number. */
136
- access_group?: number;
148
+ _what_public_see?: Record<string, any>;
149
+ /** @ignore */
150
+ services?: Record<string, any>[];
137
151
  }
138
152
 
139
153
  export type GetRecordParams = {
@@ -323,8 +337,6 @@ export type Service = {
323
337
  email_subscribers: number;
324
338
  /** Service group. 1 = free try out. 1 > paid users. */
325
339
  group: number;
326
- /** Locale of where service was created. */
327
- locale: string;
328
340
  /** Service region */
329
341
  region: string;
330
342
  /** Service name. */
@@ -368,18 +380,9 @@ export type Service = {
368
380
  users: number;
369
381
  };
370
382
 
371
- export type SubscriberGroup = {
383
+ export type SubscriptionGroup = {
372
384
  /** User id. */
373
- userId: string;
385
+ user_id: string;
374
386
  /** Target group number (1 ~ 9). '*' is given, will apply to all groups. */
375
387
  group: number | '*';
376
- };
377
-
378
- export type SubscriberFetch = {
379
- /** User id of E-Mail. Defaults to users own id. If E-Mail is given for fetch subscription, api will fetch newletter subscription.*/
380
- userId?: string;
381
- /** Target group number (1 ~ 9). Fetch all group if ommited. */
382
- group: number;
383
- /** True / False to fetch email subscribed subscriptions. If omitted, will fetch both. */
384
- emailSubscription?: boolean;
385
388
  };
package/src/skapi.ts CHANGED
@@ -14,12 +14,12 @@ import {
14
14
  UserProfile,
15
15
  PostRecordParams,
16
16
  FetchOptions,
17
- SubscriberGroup,
18
- SubscriberFetch,
17
+ SubscriptionGroup,
19
18
  FetchResponse,
20
19
  GetRecordParams,
21
20
  QueryParams,
22
- Newsletters
21
+ Newsletters,
22
+ Connection
23
23
  } from './Types';
24
24
  import SkapiError from './skapi_error';
25
25
  import { formResponse } from './decorators';
@@ -52,22 +52,6 @@ type CachedRequests = {
52
52
  };
53
53
  };
54
54
 
55
- type Connection = {
56
- /** Connection locale */
57
- locale: string;
58
- /** Name of the connected service */
59
- name: string;
60
- /** Id of the service owner */
61
- owner: string;
62
- /** E-Mail address of the service owner */
63
- owner_email: string;
64
- /** Service id */
65
- service: string;
66
- /** 13 digits timestamp of the service creation */
67
- timestamp: number;
68
- /** hash string used for login */
69
- hash?: string;
70
- };
71
55
  /**
72
56
  * All the methods used in Skapi are promises.<br>
73
57
  * Use async/await or Promise.then() to interact with backend.<br>
@@ -183,7 +167,7 @@ export default class Skapi {
183
167
 
184
168
  // skapi int range -4503599627370545 ~ 4503599627370546
185
169
 
186
- constructor(service_id: string, service_owner: string) {
170
+ constructor(service_id: string, service_owner: string, autoLogin: boolean = false) {
187
171
  if (typeof service_id !== 'string' || typeof service_owner !== 'string') {
188
172
  throw new SkapiError('"service_id" and "service_owner" should be type <string>.', { code: 'INVALID_PARAMETER' });
189
173
  }
@@ -244,12 +228,18 @@ export default class Skapi {
244
228
  ClientId: admin_endpoint.userpool_client
245
229
  });
246
230
 
247
- await Promise.all([
248
- skapi.updateServiceInformation(),
249
- skapi.authentication().updateSession({ refreshToken: !!restore?.connection }).catch(err => {
231
+ const process: any[] = [
232
+ skapi.updateConnection()
233
+ ];
234
+
235
+ if (restore?.connection || autoLogin) {
236
+ // from session reload | autoLogin
237
+ process.push(skapi.authentication().updateSession({ refreshToken: !!restore?.connection }).catch(err => {
250
238
  skapi.user = null;
251
- })
252
- ]);
239
+ }));
240
+ }
241
+
242
+ await Promise.all(process);
253
243
 
254
244
  const storeClassProperties = () => {
255
245
  if (skapi.__class_properties_has_been_cached) {
@@ -316,12 +306,14 @@ export default class Skapi {
316
306
  * @category Connection
317
307
  */
318
308
  static async connect(
319
- /** 22 character service id */
309
+ /** service ID */
320
310
  service_id: string,
321
- /** 36 character user id */
322
- service_owner: string
311
+ /** service owner's user ID */
312
+ service_owner: string,
313
+ /** Auto login user when true */
314
+ autoLogin: boolean = false
323
315
  ): Promise<Skapi> {
324
- const skapi = new Skapi(service_id, service_owner);
316
+ const skapi = new Skapi(service_id, service_owner, autoLogin);
325
317
  await skapi.__connection;
326
318
  return skapi;
327
319
  }
@@ -463,7 +455,7 @@ export default class Skapi {
463
455
  let hash = null;
464
456
 
465
457
  if (email) {
466
- hash = this.__serviceHash[email] || (await this.updateServiceInformation({ request_hash: email })).hash;
458
+ hash = this.__serviceHash[email] || (await this.updateConnection({ request_hash: email })).hash;
467
459
  }
468
460
 
469
461
  else {
@@ -2192,11 +2184,10 @@ export default class Skapi {
2192
2184
  return await this.request('subscribe-newsletter', params);
2193
2185
  }
2194
2186
 
2195
-
2196
- private async subscriptionGroupCheck(option: Record<string, any>) {
2187
+ private async subscriptionGroupCheck(option: SubscriptionGroup) {
2197
2188
  await this.__connection;
2198
2189
  option = checkParams(option, {
2199
- userId: (v: string) => validateUserId(v, '"userId"'),
2190
+ user_id: (v: string) => validateUserId(v, '"user_id"'),
2200
2191
  group: (v: number | string) => {
2201
2192
  if (v === '*') {
2202
2193
  return v;
@@ -2211,12 +2202,11 @@ export default class Skapi {
2211
2202
  }
2212
2203
 
2213
2204
  return v;
2214
- },
2215
- emailSubscription: ['boolean']
2216
- }, ['userId', 'group']);
2205
+ }
2206
+ }, ['user_id', 'group']);
2217
2207
 
2218
- if (this.user && option.userId === this.user.user_id) {
2219
- throw new SkapiError(`"userId" cannot be the user's own ID.`, { code: 'INVALID_PARAMETER' });
2208
+ if (this.user && option.user_id === this.user.user_id) {
2209
+ throw new SkapiError(`"user_id" cannot be the user's own ID.`, { code: 'INVALID_PARAMETER' });
2220
2210
  }
2221
2211
 
2222
2212
  return option;
@@ -2230,25 +2220,24 @@ export default class Skapi {
2230
2220
  * ```
2231
2221
  * // user subscribes to another user with email subscription
2232
2222
  * await skapi.subscribe({
2233
- * userId: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
2234
- * group: 1,
2235
- * emailSubscription: true
2223
+ * user_id: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
2224
+ * group: 1
2236
2225
  * })
2237
2226
  * ```
2238
2227
  * @category Subscriptions
2239
2228
  */
2240
2229
  @formResponse()
2241
2230
  async subscribe(
2242
- option: SubscriberGroup
2231
+ option: SubscriptionGroup
2243
2232
  ) {
2244
- let { userId, group } = await this.subscriptionGroupCheck(option);
2233
+ let { user_id, group } = await this.subscriptionGroupCheck(option);
2245
2234
 
2246
2235
  if (group === '*') {
2247
2236
  throw new SkapiError('Cannot subscribe to all groups at once.', { code: 'INVALID_PARAMETER' });
2248
2237
  }
2249
2238
 
2250
2239
  return await this.request('subscription', {
2251
- subscribe: userId,
2240
+ subscribe: user_id,
2252
2241
  group
2253
2242
  }, { auth: true });
2254
2243
  }
@@ -2258,18 +2247,18 @@ export default class Skapi {
2258
2247
  * ```
2259
2248
  * // user unsubscribes from another user
2260
2249
  * await skapi.unsubscribe({
2261
- * userId: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
2250
+ * user_id: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
2262
2251
  * group: 2
2263
2252
  * })
2264
2253
  * ```
2265
2254
  * @category Subscriptions
2266
2255
  */
2267
2256
  @formResponse()
2268
- async unsubscribe(option: SubscriberGroup) {
2269
- let { userId, group } = await this.subscriptionGroupCheck(option);
2257
+ async unsubscribe(option: SubscriptionGroup) {
2258
+ let { user_id, group } = await this.subscriptionGroupCheck(option);
2270
2259
 
2271
2260
  return await this.request('subscription', {
2272
- unsubscribe: userId,
2261
+ unsubscribe: user_id,
2273
2262
  group
2274
2263
  }, { auth: true });
2275
2264
  }
@@ -2279,20 +2268,20 @@ export default class Skapi {
2279
2268
  * ```
2280
2269
  * // account owner blocks user from group 2
2281
2270
  * await skapi.blockSubscriber({
2282
- * userId: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
2271
+ * user_id: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
2283
2272
  * group: 2
2284
2273
  * })
2285
2274
  * // account owner blocks user from all group
2286
2275
  * await skapi.blockSubscriber({
2287
- * userId: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
2276
+ * user_id: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
2288
2277
  * })
2289
2278
  * ```
2290
2279
  * @category Subscriptions
2291
2280
  */
2292
2281
  @formResponse()
2293
- async blockSubscriber(option: SubscriberGroup): Promise<string> {
2294
- let { userId, group } = await this.subscriptionGroupCheck(option);
2295
- return await this.request('subscription', { block: userId, group }, { auth: true });
2282
+ async blockSubscriber(option: SubscriptionGroup): Promise<string> {
2283
+ let { user_id, group } = await this.subscriptionGroupCheck(option);
2284
+ return await this.request('subscription', { block: user_id, group }, { auth: true });
2296
2285
  }
2297
2286
 
2298
2287
  /**
@@ -2300,33 +2289,33 @@ export default class Skapi {
2300
2289
  * ```
2301
2290
  * // account owner unblocks user from group 2
2302
2291
  * await skapi.unblockSubscriber({
2303
- * userId: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
2292
+ * user_id: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
2304
2293
  * group: 2
2305
2294
  * })
2306
2295
  *
2307
2296
  * // account owner unblocks user from all group
2308
2297
  * await skapi.unblockSubscriber({
2309
- * userId: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
2298
+ * user_id: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
2310
2299
  * })
2311
2300
  * ```
2312
2301
  * @category Subscriptions
2313
2302
  */
2314
2303
  @formResponse()
2315
- async unblockSubscriber(option: SubscriberGroup): Promise<string> {
2316
- let { userId, group } = await this.subscriptionGroupCheck(option);
2317
- return await this.request('subscription', { unblock: userId, group }, { auth: true });
2304
+ async unblockSubscriber(option: SubscriptionGroup): Promise<string> {
2305
+ let { user_id, group } = await this.subscriptionGroupCheck(option);
2306
+ return await this.request('subscription', { unblock: user_id, group }, { auth: true });
2318
2307
  }
2319
2308
 
2320
2309
  /**
2321
2310
  * Get user's subscriptions
2322
2311
  * @ignore
2323
2312
  */
2324
- async getUserSubscriptions(option: SubscriberFetch): Promise<FetchResponse> {
2313
+ async getUserSubscriptions(option: SubscriptionGroup): Promise<FetchResponse> {
2325
2314
  await this.__connection;
2326
2315
  option = checkParams(option, {
2327
- userId: (v: string) => {
2316
+ user_id: (v: string) => {
2328
2317
  try {
2329
- return validateUserId(v, '"userId"');
2318
+ return validateUserId(v, '"user_id"');
2330
2319
  } catch (err) {
2331
2320
  }
2332
2321
 
@@ -2337,14 +2326,12 @@ export default class Skapi {
2337
2326
 
2338
2327
  throw new SkapiError('"subscriber" should be either valid user ID or E-Mail.', { code: 'INVALID_PARAMETER' });
2339
2328
  },
2340
- group: 'number',
2341
- emailSubscription: 'boolean'
2329
+ group: 'number'
2342
2330
  }) || {};
2343
2331
 
2344
2332
  return this.getSubscriptions({
2345
- subscriber: option.userId || this.user?.user_id,
2346
- group: option.group,
2347
- emailSubscription: option?.emailSubscription || undefined
2333
+ subscriber: option.user_id || this.user?.user_id,
2334
+ group: option.group
2348
2335
  });
2349
2336
  }
2350
2337
 
@@ -2353,18 +2340,16 @@ export default class Skapi {
2353
2340
  * Get user's subscribers
2354
2341
  * @ignore
2355
2342
  */
2356
- async getUserSubscribers(option: SubscriberFetch): Promise<FetchResponse> {
2343
+ async getUserSubscribers(option: SubscriptionGroup): Promise<FetchResponse> {
2357
2344
  await this.__connection;
2358
2345
  option = checkParams(option, {
2359
- userId: (v: string) => validateUserId(v, '"userId"'),
2360
- group: 'number',
2361
- emailSubscription: 'boolean'
2346
+ user_id: (v: string) => validateUserId(v, '"user_id"'),
2347
+ group: 'number'
2362
2348
  }) || {};
2363
2349
 
2364
2350
  let subParams = {
2365
- subscription: option.userId || this.user?.user_id,
2366
- group: option.group,
2367
- emailSubscription: option.emailSubscription
2351
+ subscription: option.user_id || this.user?.user_id,
2352
+ group: option.group
2368
2353
  };
2369
2354
 
2370
2355
  return this.getSubscriptions(subParams);
@@ -2382,9 +2367,7 @@ export default class Skapi {
2382
2367
  /** Subscription id. User id that subscriber has subscribed to. */
2383
2368
  subscription?: string;
2384
2369
  /** subscription group. if omitted, will fetch all groups. */
2385
- group?: number;
2386
- /** True | False to fetch service email subscribers. If omitted, will fetch all subscribers. */
2387
- emailSubscription?: boolean;
2370
+ group?: number | '*';
2388
2371
  /** Fetch blocked subscription when True */
2389
2372
  blocked?: boolean;
2390
2373
  },
@@ -2424,7 +2407,6 @@ export default class Skapi {
2424
2407
 
2425
2408
  throw new SkapiError('"subscriber" should be either valid service ID or user ID.', { code: 'INVALID_PARAMETER' });
2426
2409
  },
2427
- emailSubscription: 'boolean',
2428
2410
  blocked: 'boolean'
2429
2411
  });
2430
2412
 
@@ -2613,7 +2595,7 @@ export default class Skapi {
2613
2595
  */
2614
2596
  @formResponse()
2615
2597
  async signup(
2616
- form: Form | UserProfile | { email: String, password: String; },
2598
+ form: Form | UserProfile & { email: String, password: String; },
2617
2599
  option?: {
2618
2600
  /**
2619
2601
  * When true, the service will send out confirmation E-Mail.
@@ -3166,7 +3148,7 @@ export default class Skapi {
3166
3148
 
3167
3149
  // set alternative signin email
3168
3150
  if (params.email) {
3169
- let connect = await this.updateServiceInformation({ request_hash: params.email });
3151
+ let connect = await this.updateConnection({ request_hash: params.email });
3170
3152
  params['preferred_username'] = connect.hash;
3171
3153
  }
3172
3154
 
@@ -3556,7 +3538,7 @@ export default class Skapi {
3556
3538
  return user;
3557
3539
  }
3558
3540
 
3559
- private async updateServiceInformation(params?: { request_hash: string; }): Promise<Connection> {
3541
+ private async updateConnection(params?: { request_hash: string; }): Promise<Connection> {
3560
3542
 
3561
3543
  let request = null;
3562
3544
  let connectedService: Record<string, any> = {};
package/src/utils.ts CHANGED
@@ -298,8 +298,8 @@ function checkParams(
298
298
  val = _params;
299
299
  }
300
300
 
301
- if (val === undefined) {
302
- throw errToThrow || new SkapiError(`Type "undefined"${isInvalid}`, { code: 'INVALID_PARAMETER' });
301
+ if (val === undefined && errToThrow) {
302
+ throw errToThrow;
303
303
  }
304
304
 
305
305
  return val;