@vtecx/vtecxnext 2.2.4 → 2.2.6

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,27 @@ 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
+ getPageWithPagination: (uri: string, num: number, targetService?: string) => Promise<any>;
411
+ /**
412
+ * ページングのカーソルリスト作成処理
413
+ * 続きがある場合、次のカーソルリスト作成処理を実行する
414
+ * @param vtecxnext
415
+ * @param uri キーとパラメータ
416
+ * @param prevLastPage 前回の最終ページ
417
+ * @param targetService 対象サービス
418
+ */
419
+ private nextPagination;
402
420
  /**
403
421
  * post data to bigquery
404
422
  * @param feed entries (JSON)
@@ -985,3 +1003,10 @@ export declare class VtecxNextError extends Error {
985
1003
  export declare class FetchError extends VtecxNextError {
986
1004
  constructor(message: string);
987
1005
  }
1006
+ /**
1007
+ * VtecxNextError型かどうかチェック
1008
+ * インターフェースの判定には型ガード関数を使う
1009
+ * @param value チェックオブジェクト
1010
+ * @returns VtecxNextError型の場合true
1011
+ */
1012
+ 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,60 @@ 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
+ getPageWithPagination = async (uri, num, targetService) => {
1434
+ //console.log(`[getPageWithPagination] start. uri=${uri} num=${num} ${targetService ? 'targetService=' + targetService : ''}`)
1435
+ // ページ数が1の場合、カーソルリスト作成処理を行う
1436
+ if (num === 1) {
1437
+ //console.log(`[getPageWithPagination] pagination start. uri=${uri}`)
1438
+ const paginationInfo = await this.pagination(uri, `1,${String(PAGINATION_NUM)}`, targetService);
1439
+ if (paginationInfo.hasNext) {
1440
+ // 次のカーソルリスト作成 (非同期のまま)
1441
+ this.nextPagination(uri, PAGINATION_NUM, targetService);
1442
+ }
1443
+ if (paginationInfo.lastPageNumber === 0) {
1444
+ // データが存在しない場合終了
1445
+ return undefined;
1446
+ }
1447
+ }
1448
+ // ページ取得
1449
+ //console.log(`[getPageWithPagination] getPage start. uri=${uri} num=${num}`)
1450
+ try {
1451
+ return await this.getPage(uri, num, targetService);
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
+ * @param targetService 対象サービス
1470
+ */
1471
+ nextPagination = async (uri, prevLastPage, targetService) => {
1472
+ const firstPage = prevLastPage + 1;
1473
+ const lastPage = prevLastPage + prevLastPage;
1474
+ const paginationInfo = await this.pagination(uri, `${String(firstPage)},${String(lastPage)}`, targetService);
1475
+ if (paginationInfo.hasNext) {
1476
+ await this.nextPagination(uri, lastPage, targetService);
1477
+ }
1478
+ };
1423
1479
  /**
1424
1480
  * post data to bigquery
1425
1481
  * @param feed entries (JSON)
@@ -4011,6 +4067,29 @@ class FetchError extends VtecxNextError {
4011
4067
  }
4012
4068
  }
4013
4069
  exports.FetchError = FetchError;
4070
+ /**
4071
+ * VtecxNextError型かどうかチェック
4072
+ * インターフェースの判定には型ガード関数を使う
4073
+ * @param value チェックオブジェクト
4074
+ * @returns VtecxNextError型の場合true
4075
+ */
4076
+ const isVtecxNextError = (value) => {
4077
+ // 値がオブジェクトであるかの判定
4078
+ if (typeof value !== "object" || value === null) {
4079
+ return false;
4080
+ }
4081
+ const { status, message } = value;
4082
+ // statusプロパティーが数値型かを判定
4083
+ if (typeof status !== "number") {
4084
+ return false;
4085
+ }
4086
+ // messageプロパティーが文字列型かを判定
4087
+ if (typeof message !== "string") {
4088
+ return false;
4089
+ }
4090
+ return true;
4091
+ };
4092
+ exports.isVtecxNextError = isVtecxNextError;
4014
4093
  //---------------------------------------------
4015
4094
  /**
4016
4095
  * 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.6",
4
4
  "description": "vte.cx Next.js api",
5
5
  "main": "dist/index.js",
6
6
  "files": [