@vtecx/vtecxnext 2.2.4 → 2.2.5

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.
@@ -396,9 +396,26 @@ export declare class VtecxNext {
396
396
  * @param uri key and conditions
397
397
  * @param num page number
398
398
  * @param targetService target service name (for service linkage)
399
- * @return feed Maximum number of pages in the specified page range, and total count.
399
+ * @return feed (entry array)
400
400
  */
401
401
  getPage: (uri: string, num: number, targetService?: string) => Promise<any>;
402
+ /**
403
+ * practical paging
404
+ * If you specify page 1, a new cursor list will be created.
405
+ * @param uri key and conditions
406
+ * @param num page number
407
+ * @param targetService target service name (for service linkage)
408
+ * @return feed (entry array)
409
+ */
410
+ practicalPaging: (uri: string, num: number, targetService?: string) => Promise<any>;
411
+ /**
412
+ * ページングのカーソルリスト作成処理
413
+ * 続きがある場合、次のカーソルリスト作成処理を実行する
414
+ * @param vtecxnext
415
+ * @param uri キーとパラメータ
416
+ * @param prevLastPage 前回の最終ページ
417
+ */
418
+ private nextPagination;
402
419
  /**
403
420
  * post data to bigquery
404
421
  * @param feed entries (JSON)
@@ -985,3 +1002,10 @@ export declare class VtecxNextError extends Error {
985
1002
  export declare class FetchError extends VtecxNextError {
986
1003
  constructor(message: string);
987
1004
  }
1005
+ /**
1006
+ * VtecxNextError型かどうかチェック
1007
+ * インターフェースの判定には型ガード関数を使う
1008
+ * @param value チェックオブジェクト
1009
+ * @returns VtecxNextError型の場合true
1010
+ */
1011
+ export declare const isVtecxNextError: (value: unknown) => value is VtecxNextError;
package/dist/vtecxnext.js CHANGED
@@ -26,7 +26,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
26
26
  return (mod && mod.__esModule) ? mod : { "default": mod };
27
27
  };
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
- exports.FetchError = exports.VtecxNextError = exports.VtecxResponse = exports.VtecxNext = exports.hello = void 0;
29
+ exports.isVtecxNextError = exports.FetchError = exports.VtecxNextError = exports.VtecxResponse = exports.VtecxNext = exports.hello = void 0;
30
30
  const sqlstring_1 = __importDefault(require("sqlstring"));
31
31
  const url_1 = __importStar(require("url"));
32
32
  /**
@@ -45,6 +45,8 @@ const SERVLETPATH_PROVIDER = '/p';
45
45
  const SERVLETPATH_OAUTH = '/o';
46
46
  /** header : nextpage */
47
47
  const HEADER_NEXTPAGE = 'x-vtecx-nextpage';
48
+ /** The number of cursors to create (for practical paging) */
49
+ const PAGINATION_NUM = 7;
48
50
  class VtecxNext {
49
51
  /** Request */
50
52
  req;
@@ -1395,7 +1397,7 @@ class VtecxNext {
1395
1397
  * @param uri key and conditions
1396
1398
  * @param num page number
1397
1399
  * @param targetService target service name (for service linkage)
1398
- * @return feed Maximum number of pages in the specified page range, and total count.
1400
+ * @return feed (entry array)
1399
1401
  */
1400
1402
  getPage = async (uri, num, targetService) => {
1401
1403
  //console.log(`[vtecxnext getPage] start. uri=${uri} num=${num}`)
@@ -1420,6 +1422,59 @@ class VtecxNext {
1420
1422
  // 戻り値
1421
1423
  return await getJson(response);
1422
1424
  };
1425
+ /**
1426
+ * practical paging
1427
+ * If you specify page 1, a new cursor list will be created.
1428
+ * @param uri key and conditions
1429
+ * @param num page number
1430
+ * @param targetService target service name (for service linkage)
1431
+ * @return feed (entry array)
1432
+ */
1433
+ practicalPaging = async (uri, num, targetService) => {
1434
+ //console.log(`[practicalPaging] start. uri=${uri} num=${num} ${targetService ? 'targetService=' + targetService : ''}`)
1435
+ // ページ数が1の場合、カーソルリスト作成処理を行う
1436
+ if (num === 1) {
1437
+ //console.log(`[practicalPaging] pagination start. uri=${uri}`)
1438
+ const paginationInfo = await this.pagination(uri, `1,${String(PAGINATION_NUM)}`);
1439
+ if (paginationInfo.hasNext) {
1440
+ // 次のカーソルリスト作成 (非同期のまま)
1441
+ this.nextPagination(uri, PAGINATION_NUM);
1442
+ }
1443
+ if (paginationInfo.lastPageNumber === 0) {
1444
+ // データが存在しない場合終了
1445
+ return undefined;
1446
+ }
1447
+ }
1448
+ // ページ取得
1449
+ //console.log(`[practicalPaging] getPage start. uri=${uri} num=${num}`)
1450
+ try {
1451
+ return await this.getPage(uri, num);
1452
+ }
1453
+ catch (error) {
1454
+ if ((0, exports.isVtecxNextError)(error)) {
1455
+ // ステータス400で「There is no designated page. The last page: ページ数」の場合、空データを返す。
1456
+ if (error.status === 400 && error.message.startsWith('There is no designated page.')) {
1457
+ return undefined;
1458
+ }
1459
+ }
1460
+ throw error;
1461
+ }
1462
+ };
1463
+ /**
1464
+ * ページングのカーソルリスト作成処理
1465
+ * 続きがある場合、次のカーソルリスト作成処理を実行する
1466
+ * @param vtecxnext
1467
+ * @param uri キーとパラメータ
1468
+ * @param prevLastPage 前回の最終ページ
1469
+ */
1470
+ nextPagination = async (uri, prevLastPage) => {
1471
+ const firstPage = prevLastPage + 1;
1472
+ const lastPage = prevLastPage + prevLastPage;
1473
+ const paginationInfo = await this.pagination(uri, `${String(firstPage)},${String(lastPage)}`);
1474
+ if (paginationInfo.hasNext) {
1475
+ await this.nextPagination(uri, lastPage);
1476
+ }
1477
+ };
1423
1478
  /**
1424
1479
  * post data to bigquery
1425
1480
  * @param feed entries (JSON)
@@ -4011,6 +4066,29 @@ class FetchError extends VtecxNextError {
4011
4066
  }
4012
4067
  }
4013
4068
  exports.FetchError = FetchError;
4069
+ /**
4070
+ * VtecxNextError型かどうかチェック
4071
+ * インターフェースの判定には型ガード関数を使う
4072
+ * @param value チェックオブジェクト
4073
+ * @returns VtecxNextError型の場合true
4074
+ */
4075
+ const isVtecxNextError = (value) => {
4076
+ // 値がオブジェクトであるかの判定
4077
+ if (typeof value !== "object" || value === null) {
4078
+ return false;
4079
+ }
4080
+ const { status, message } = value;
4081
+ // statusプロパティーが数値型かを判定
4082
+ if (typeof status !== "number") {
4083
+ return false;
4084
+ }
4085
+ // messageプロパティーが文字列型かを判定
4086
+ if (typeof message !== "string") {
4087
+ return false;
4088
+ }
4089
+ return true;
4090
+ };
4091
+ exports.isVtecxNextError = isVtecxNextError;
4014
4092
  //---------------------------------------------
4015
4093
  /**
4016
4094
  * vte.cxへリクエスト
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vtecx/vtecxnext",
3
- "version": "2.2.4",
3
+ "version": "2.2.5",
4
4
  "description": "vte.cx Next.js api",
5
5
  "main": "dist/index.js",
6
6
  "files": [