Skip to content

Commit

Permalink
add select
Browse files Browse the repository at this point in the history
  • Loading branch information
dzencot committed Sep 16, 2024
1 parent 3aa2d73 commit 2091f09
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 70 deletions.
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"@fastify/swagger": "^8.15.0",
"@fastify/swagger-ui": "^4.1.0",
"fastify": "^4.28.1",
"lodash": "^4.17.21",
"openapi-backend": "^5.10.6",
"openapi-client-axios": "^7.5.5",
"swagger-ui-dist": "^5.17.14"
Expand Down
68 changes: 16 additions & 52 deletions server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import fastify from 'fastify';
import path from 'node:path';
import fastifySwagger from '@fastify/swagger';
import swaggerUI from '@fastify/swagger-ui';
import { getInitData, getId, getToken } from './utils.js';
import { getInitData, getId, getToken, prepareListData, prepareItem } from './utils.js';

const title = 'Forum HTTP Api Example';
const { dirname } = import.meta;
Expand Down Expand Up @@ -41,40 +41,25 @@ const app = async (host, port) => {
state.users.push(user);
return res.status(200).send(user);
},
UserService_list: (c, req, res) => {
const skip = parseInt(c.request.query.skip ?? 0, 10)
const limit = parseInt(c.request.query.limit ?? 100, 10);
const users = state.users.slice(skip, skip + limit);
res.status(200).send({
users,
total: state.users.length,
skip,
limit,
});
},
UserService_list: (c, req, res) => res.status(200).send(prepareListData('users', state, c)),
UserService_get: (c, req, res) => {
const { id } = c.request.params;
const { select } = context.request.query.select ?? [];
const user = state.users.find((item) => item.id === id);
if (!user) {
return res.status(404).send({ code: 404, message: 'Not found' });
}
return res.status(200).send(user);
return res.status(200).send(prepareItem(user, select));
},
UserService_getPosts: (c, req, res) => {
const { authorId } = c.request.params;
const posts = state.posts.filter((item) => item.authorId === authorId);
const skip = parseInt(c.request.query.skip ?? 0, 10)
const limit = parseInt(c.request.query.limit ?? 100, 10);
const page = posts.slice(skip, skip + limit);
return res.status(200).send({ posts: page, skip, limit, total: posts.length });
const data = prepareListData('posts', state, c, (item) => item.authorId === authorId);
return res.status(200).send(data);
},
UserService_getComments: (c, req, res) => {
const { authorId } = c.request.params;
const comments = state.comments.filter((item) => item.authorId === authorId);
const skip = parseInt(c.request.query.skip ?? 0, 10)
const limit = parseInt(c.request.query.limit ?? 100, 10);
const page = comments.slice(skip, skip + limit);
return res.status(200).send({ comments: page, skip, limit, total: comments.length });
const data = prepareListData('comments', state, c, (item) => item.authorId === authorId);
return res.status(200).send(data);
},
UserService_update: (c, req, res) => {
const { id } = c.request.params;
Expand Down Expand Up @@ -113,32 +98,20 @@ const app = async (host, port) => {
state.posts.push(post);
return res.status(200).send(post);
},
PostService_list: (c, req, res) => {
const skip = parseInt(c.request.query.skip ?? 0, 10);
const limit = parseInt(c.request.query.limit ?? 100, 10);
const posts = state.posts.slice(skip, skip + limit);
res.status(200).send({
posts,
total: state.posts.length,
skip,
limit,
});
},
PostService_list: (c, req, res) => prepareListData('posts', state, c),
PostService_get: (c, req, res) => {
const { id } = c.request.params;
const { select } = context.request.query.select ?? [];
const post = state.posts.find((item) => item.id === id);
if (!post) {
return res.status(404).send({ code: 404, message: 'Not found' });
}
return res.status(200).send(post);
return res.status(200).send(prepareItem(post, select));
},
PostService_getComments: (c, req, res) => {
const { postId } = c.request.params;
const comments = state.comments.filter((item) => item.postId === postId);
const skip = parseInt(c.request.query.skip ?? 0, 10)
const limit = parseInt(c.request.query.limit ?? 100, 10);
const page = comments.slice(skip, skip + limit);
return res.status(200).send({ comments: page, skip, limit, total: comments.length });
const data = prepareListData('comments', state, c, (item) => item.postId === postId);
return res.status(200).send(data);
},
PostService_update: (c, req, res) => {
const { id } = c.request.params;
Expand Down Expand Up @@ -175,24 +148,15 @@ const app = async (host, port) => {
state.comments.push(comment);
return res.status(200).send(comment);
},
CommentService_list: (c, req, res) => {
const skip = parseInt(c.request.query.skip ?? 0, 10);
const limit = parseInt(c.request.query.limit ?? 100, 10);
const comments = state.comments.slice(skip, skip + limit);
res.status(200).send({
comments,
total: state.comments.length,
skip,
limit,
});
},
CommentService_list: (c, req, res) => prepareListData('comments', state, c),
CommentService_get: (c, req, res) => {
const { id } = c.request.params;
const { select } = context.request.query.select ?? [];
const comment = state.comments.find((item) => item.id === id);
if (!comment) {
return res.status(404).send({ code: 404, message: 'Not found' });
}
return res.status(200).send(comment);
return res.status(200).send(prepareItem(comment, select));
},
CommentService_update: (c, req, res) => {
const { id } = c.request.params;
Expand Down
36 changes: 33 additions & 3 deletions server/utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { faker } from '@faker-js/faker';
import _ from 'lodash';

const listSize = 10;
const defaultLimit = 30;

const createUser = () => ({
id: faker.string.uuid(),
Expand Down Expand Up @@ -26,21 +30,21 @@ export const getToken = () => faker.string.alphanumeric(120);
export const getInitData = () => {
const users = [];

for(let i = 1; i <= 10; i++) {
for(let i = 1; i <= listSize; i++) {
users.push(createUser());
}

const posts = users.map((user) => {
const userPosts = [];
for(let i = 1; i <= 10; i++) {
for(let i = 1; i <= listSize; i++) {
userPosts.push(createPost(user));
}
return userPosts;
}).flat();

const comments = posts.map((post) => {
const postComments = [];
for(let i = 1; i <= 10; i++) {
for(let i = 1; i <= listSize; i++) {
postComments.push(createComment(post));
}
return postComments;
Expand All @@ -54,4 +58,30 @@ export const getInitData = () => {
};
};

export const prepareItem = (item, selectors = []) => {
if (selectors) {
return {
id: item.id,
..._.pick(item, selectors),
};
}
return item;
};

export const prepareListData = (name, state, context, callbackFilter = () => true) => {
const skip = parseInt(context.request.query.skip ?? 0, 10)
const limit = parseInt(context.request.query.limit ?? defaultLimit, 10);
const { select } = context.request.query ?? [];
let list = state[name].filter(callbackFilter).slice(skip, skip + limit);
if (select) {
list = list.map((item) => prepareItem(item, select));
}
return {
[name]: list,
total: list.length,
skip,
limit,
};
};

export const getId = () => faker.string.uuid();
2 changes: 1 addition & 1 deletion typescpec/models/comment.tsp
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ model Comments {
comments: Comment[];
total: integer;
skip: integer;
limit: integer;
limit: integer = 30;
}
2 changes: 1 addition & 1 deletion typescpec/models/post.tsp
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ model Posts {
posts: Post[];
total: integer;
skip: integer;
limit: integer;
limit: integer = 30;
}
2 changes: 1 addition & 1 deletion typescpec/models/user.tsp
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ model Users {
users: User[];
total: integer;
skip: integer;
limit: integer;
limit: integer = 30;
}
11 changes: 9 additions & 2 deletions typescpec/services/commentsService.tsp
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,17 @@ namespace ForumService;
@route("/comments")
interface CommentService {
@get
op list(@query skip?: string, @query limit?: string): Comments | Error;
op list(
@query skip?: integer,
@query limit?: integer = 30,
@query select?: string
): Comments | Error;

@get
op get(@path id: string): Comment | Error;
op get(
@path id: string,
@query select?: string
): Comment | Error;

@useAuth(BearerAuth)
@post
Expand Down
16 changes: 12 additions & 4 deletions typescpec/services/postsService.tsp
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,17 @@ namespace ForumService;
@route("/posts")
interface PostService {
@get
op list(@query skip?: string, @query limit?: string): Posts | Error;
op list(
@query skip?: integer,
@query limit?: integer = 30,
@query select?: string
): Posts | Error;

@get
op get(@path id: string): Post | Error;
op get(
@path id: string,
@query select?: string
): Post | Error;

@useAuth(BearerAuth)
@post
Expand All @@ -32,7 +39,8 @@ interface PostService {
@route("/{postId}/comments")
op getComments(
@path postId: string,
@query skip?: string,
@query limit?: string
@query skip?: integer,
@query limit?: integer = 30,
@query select?: string
): Comments | Error;
}
21 changes: 15 additions & 6 deletions typescpec/services/usersService.tsp
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,17 @@ namespace ForumService;
@route("/users")
interface UserService {
@get
op list(@query skip?: string, @query limit?: string): Users | Error;
op list(
@query skip?: string,
@query limit?: string,
@query select?: string[]
): Users | Error;

@get
op get(@path id: string): User | Error;
op get(
@path id: string,
@query select?: string
): User | Error;

@useAuth(BearerAuth)
@post
Expand All @@ -35,14 +42,16 @@ interface UserService {
@route("/{authorId}/posts")
op getPosts(
@path authorId: string,
@query skip?: string,
@query limit?: string
@query skip?: integer,
@query limit?: integer = 30,
@query select?: string
): Posts | Error;

@route("/{authorId}/comments")
op getComments(
@path authorId: string,
@query skip?: string,
@query limit?: string
@query skip?: integer,
@query limit?: integer = 30,
@query select?: string
): Comments | Error;
}

0 comments on commit 2091f09

Please sign in to comment.