Skip to content

Commit

Permalink
fix(nodejs): added pagination to the Viron feature
Browse files Browse the repository at this point in the history
  • Loading branch information
takoring committed Feb 10, 2025
1 parent e1a1265 commit ceebb3f
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 14 deletions.
2 changes: 1 addition & 1 deletion example/nodejs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"dependencies": {
"@aws-sdk/client-s3": "^3.451.0",
"@types/sanitize-html": "^2.13.0",
"@viron/lib": "2.4.0-alpha.6",
"@viron/lib": "2.4.0-alpha.7",
"accepts": "^1.3.7",
"compression": "^1.7.4",
"cookie-parser": "^1.4.5",
Expand Down
5 changes: 4 additions & 1 deletion example/nodejs/src/controllers/adminroles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ import { RouteContext } from '../application';
export const listVironAdminRoles = async (
context: RouteContext
): Promise<void> => {
const { size, page } = context.params.query;
const result = await domainsAdminRole.listByOas(
context.req._context.apiDefinition
context.req._context.apiDefinition,
size,
page
);

context.res.json(result);
Expand Down
4 changes: 2 additions & 2 deletions example/nodejs/src/controllers/resources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ export const downloadResources = async (
context: RouteContext
): Promise<void> => {
const { resourceName } = context.params.path;
const { format = 'json' } = context.params.query;
const result = await exportResources(resourceName, format);
const { format = 'json', size, page, sort } = context.params.query;
const result = await exportResources(resourceName, format, size, page, sort);
context.res.header(HTTP_HEADER.CONTENT_TYPE, contentType(format) || '');
context.res.header(
HTTP_HEADER.CONTENT_DISPOSITION,
Expand Down
16 changes: 10 additions & 6 deletions example/nodejs/src/domains/resources.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
import { TABLE_SORT_DELIMITER, TABLE_SORT_ORDER } from '@viron/lib';
import { getRepository, RepositoryNames } from '../repositories';

// データエクスポート
export const exportResources = async (
resourceName: string,
format: 'json' | 'csv'
format: 'json' | 'csv',
size?: number,
page?: number,
sort = [`createdAt${TABLE_SORT_DELIMITER}${TABLE_SORT_ORDER.DESC}`]
// eslint-disable-next-line @typescript-eslint/ban-types
): Promise<object | string> => {
const repository = getRepository(resourceName as RepositoryNames);
if (!repository) {
return format === 'json' ? {} : '';
}

const result = await repository.find();
const result = await repository.findWithPager({}, size, page, sort);
switch (format) {
case 'csv': {
if (!result.length) {
if (!result.list.length) {
return '';
}
const headers = Object.keys(result[0]);
const headers = Object.keys(result.list[0]);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return result.reduce((ret: string, doc: any) => {
return result.list.reduce((ret: string, doc: any) => {
const line: unknown[] = [];
Object.entries(doc).forEach(([k, v]) => {
line[headers.indexOf(k)] = v;
Expand All @@ -28,6 +32,6 @@ export const exportResources = async (
}, headers.join(','));
}
default:
return result;
return result.list;
}
};
3 changes: 3 additions & 0 deletions example/nodejs/src/openapi/resources.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ paths:
parameters:
- $ref: '#/components/parameters/ResourceNamePathParam'
- $ref: '#/components/parameters/FormatQueryParam'
- $ref: './components.yaml#/components/parameters/PagerSizeQueryParam'
- $ref: './components.yaml#/components/parameters/PagerPageQueryParam'
- $ref: './components.yaml#/components/parameters/SortQueryParam'
responses:
200:
description: OK
Expand Down
2 changes: 1 addition & 1 deletion packages/nodejs/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@viron/lib",
"version": "2.4.0-alpha.6",
"version": "2.4.0-alpha.7",
"scripts": {
"build": "npm run clean && tsc --project tsconfig.json && cp -fr src/openapi dist/",
"clean": "rm -rf dist && rm -f tsconfig.tsbuildinfo",
Expand Down
9 changes: 6 additions & 3 deletions packages/nodejs/src/domains/adminrole.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ import {
OAS_X_PAGE_CONTENTS,
OAS_X_PAGE_CONTENT_RESOURCE_ID,
CASBIN_SYNC_INTERVAL_MSEC,
DEFAULT_PAGER_SIZE,
DEFAULT_PAGER_PAGE,
} from '../constants';
import { ListWithPager, paging } from '../helpers';
import { repositoryContainer } from '../repositories';
import { findOperation, getResourceId, VironOpenAPIObject } from './oas';
import { getDebug } from '../logging';

const debug = getDebug('domains:adminrole');

export interface AdminRolePermission {
Expand Down Expand Up @@ -301,7 +302,9 @@ export const listResourcesByOas = (oas: VironOpenAPIObject): string[] => {

// 管理ロール一覧
export const listByOas = async (
oas: VironOpenAPIObject
oas: VironOpenAPIObject,
size = DEFAULT_PAGER_SIZE,
page = DEFAULT_PAGER_PAGE
): Promise<ListWithPager<AdminRole>> => {
const policies = await listPolicies();
const resourceIds = listResourcesByOas(oas);
Expand All @@ -328,7 +331,7 @@ export const listByOas = async (
}),
};
});
return paging(result, result.length);
return paging(result, size, page);
};

// 1件作成
Expand Down
4 changes: 4 additions & 0 deletions packages/nodejs/src/openapi/adminroles.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ info:
contents:
- title: 管理ロール
operationId: listVironAdminRoles
pagination: true
type: table
resourceId: vironAdminRole

Expand All @@ -27,6 +28,9 @@ paths:
- vironAdminRole
summary: list admin roles
description: 管理ロール一覧
parameters:
- $ref: './components.yaml#/components/parameters/VironPagerSizeQueryParam'
- $ref: './components.yaml#/components/parameters/VironPagerPageQueryParam'
responses:
200:
description: OK
Expand Down
1 change: 1 addition & 0 deletions packages/nodejs/src/openapi/adminusers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ info:
- title: 管理ユーザー
type: table
operationId: listVironAdminUsers
pagination: true
resourceId: vironAdminUser

tags:
Expand Down

0 comments on commit ceebb3f

Please sign in to comment.