Skip to content

Commit

Permalink
Merge pull request #62 from GuoXiCheng/main
Browse files Browse the repository at this point in the history
publish 1.0.18
  • Loading branch information
GuoXiCheng authored Jan 2, 2024
2 parents 3c11ca9 + 1ee1e48 commit f64d236
Show file tree
Hide file tree
Showing 17 changed files with 317 additions and 6 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tiny-crud",
"version": "1.0.17",
"version": "1.0.18",
"description": "",
"main": "dist/bundle.cjs.js",
"module": "dist/bundle.esm.js",
Expand Down
18 changes: 17 additions & 1 deletion src/__tests__/book-storage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,13 @@ describe('Test Book Storage', () => {
expect(findResult.length).toEqual(1);

const findByIdResult = await Book.findById(findResult[0].id);
expect(findByIdResult).toEqual(findResult[0]);
expect(findByIdResult.id).toEqual(findResult[0].id);
expect(findByIdResult.book_name).toEqual(findResult[0].book_name);
expect(findByIdResult.book_author).toEqual(findResult[0].book_author);
expect(findByIdResult.book_price).toEqual(findResult[0].book_price);
expect(findByIdResult.created_at).toEqual(findResult[0].created_at);
expect(findByIdResult.updated_at).toEqual(findResult[0].updated_at);
expect(findByIdResult.user).not.toBeNull();
});

test('Test updateById Book', async () => {
Expand Down Expand Up @@ -167,4 +173,14 @@ describe('Test Book Storage', () => {
const lastPage = await Book.find({ page: 4, per_page: 3 });
expect(lastPage.length).toEqual(1);
});

test('Test get Book Detail', async()=>{
const result = await Book.detail();
const {id, issue_number, comments, created_at, updated_at} = result;
expect(id).not.toBeNull();
expect(issue_number).not.toBeNull();
expect(comments).not.toBeNull();
expect(created_at).not.toBeNull();
expect(updated_at).not.toBeNull();
});
});
11 changes: 11 additions & 0 deletions src/__tests__/chat-storage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ describe('Use Gitlab Test Chat Storage', () => {

const findByIdResult = await Chat.findById(findResult[0].id);
expect(findByIdResult).toEqual(findResult[0]);
expect(findByIdResult.user).not.toBeNull();
});

test('Test updateById Chat', async () => {
Expand Down Expand Up @@ -223,4 +224,14 @@ describe('Use Gitlab Test Chat Storage', () => {
});
expect(findAsc.map(item => item.id)).toEqual(findDesc.map(item => item.id).reverse());
});

test('Test get Chat Detail', async()=>{
const result = await Chat.detail();
const {id, issue_number, comments, created_at, updated_at} = result;
expect(id).not.toBeNull();
expect(issue_number).not.toBeNull();
expect(comments).not.toBeNull();
expect(created_at).not.toBeNull();
expect(updated_at).not.toBeNull();
});
});
11 changes: 11 additions & 0 deletions src/__tests__/user-storage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ describe('Test User Storage', () => {

const findByIdResult = await User.findById(findResult[0].id);
expect(findByIdResult).toEqual(findResult[0]);
expect(findByIdResult.user).not.toBeNull();
});

test('Test updateById User', async () => {
Expand Down Expand Up @@ -150,4 +151,14 @@ describe('Test User Storage', () => {
const lastPage = await User.find({ page: 4, per_page: 3 });
expect(lastPage.length).toEqual(1);
});

test('Test get User Detail', async()=>{
const result = await User.detail();
const {id, issue_number, comments, created_at, updated_at} = result;
expect(id).not.toBeNull();
expect(issue_number).not.toBeNull();
expect(comments).not.toBeNull();
expect(created_at).not.toBeNull();
expect(updated_at).not.toBeNull();
});
});
1 change: 1 addition & 0 deletions src/storage-lib/base/base-comment.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { BaseModel } from "./base-model";

export interface BaseComment extends BaseModel {
[x: string]: any;
body: string;
}
3 changes: 3 additions & 0 deletions src/storage-lib/base/base-model.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { User } from "./user";

export interface BaseModel {
id: number;
created_at: string;
updated_at: string;
user: User;
}
8 changes: 7 additions & 1 deletion src/storage-lib/base/base-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { BaseModel } from "./base-model";
import { BaseParams } from "./base-params";
import { PlainObject } from "./plain-object";
import { RouteType } from "./route-type";
import { User } from "./user";

export abstract class BaseStorage<T extends BaseModel> {
public readonly useEncrypt: boolean;
Expand All @@ -19,6 +20,8 @@ export abstract class BaseStorage<T extends BaseModel> {
this.decryptFn = request.decryptFn;
}

protected abstract extractUser(comment: BaseComment): User | null;

protected getRoute(routeType: keyof typeof RouteType, id?: number): string {
switch (routeType) {
case RouteType.find:
Expand All @@ -28,6 +31,8 @@ export abstract class BaseStorage<T extends BaseModel> {
case RouteType.updateById:
case RouteType.deleteById:
return `${this.endpoint}/issues/comments/${id}`;
case RouteType.detail:
return `${this.endpoint}/issues/${this.issueNumber}`
default:
throw new Error(`routeType ${routeType} is not supported`);
}
Expand Down Expand Up @@ -145,7 +150,8 @@ export abstract class BaseStorage<T extends BaseModel> {
} else {
obj = JSON.parse(body);
}
return {id, ...obj, created_at, updated_at}
const user = this.extractUser(comment);
return {id, ...obj, created_at, updated_at, user}
} catch (error) {
console.error(error);
throw new Error(`can not deserialize ${comment}`);
Expand Down
7 changes: 7 additions & 0 deletions src/storage-lib/base/issue-detail.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export type IssueDetail = {
id: number;
issue_number: string;
comments: number;
created_at: string;
updated_at: string;
}
3 changes: 2 additions & 1 deletion src/storage-lib/base/route-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ export enum RouteType {
findById = 'findById',
create = 'create',
updateById = 'updateById',
deleteById = 'deleteById'
deleteById = 'deleteById',
detail = 'detail'
}
5 changes: 5 additions & 0 deletions src/storage-lib/base/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export type User = {
id: number;
name: string;
avatar_url: string;
}
54 changes: 54 additions & 0 deletions src/storage-lib/gitee/gitee-detail.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
export type GiteeDetail = {
id: number,
url: string,
repository_url: string,
labels_url: string,
comments_url: string,
html_url: string,
parent_url: string,
number: string,
parent_id: number,
depth: number,
state: string,
title: string,
body: string,
user: {
id: number,
login: string,
name: string,
avatar_url: string,
url: string,
html_url: string,
remark: string,
followers_url: string,
following_url: string,
gists_url: string,
starred_url: string,
subscriptions_url: string,
organizations_url: string,
repos_url: string,
events_url: string,
received_events_url: string,
type: string
},
labels: [],
assignee: null,
collaborators: [],
repository: object,
milestone: string,
created_at: string,
updated_at: string,
plan_started_at: string,
deadline: string,
finished_at: string,
scheduled_time: number,
comments: number,
priority: number,
issue_type: string,
program: string,
security_hole: boolean,
issue_state: string,
branch: string,
issue_type_detail: object,
issue_state_detail: object
}
34 changes: 34 additions & 0 deletions src/storage-lib/gitee/gitee-storage.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { BaseRequest } from "../../request-lib";
import { BaseComment } from "../base/base-comment";
import { BaseModel } from "../base/base-model";
import { BaseStorage } from "../base/base-storage";
import { IssueDetail } from "../base/issue-detail";
import { RouteType } from "../base/route-type";
import { User } from "../base/user";
import { GiteeDetail } from "./gitee-detail";
import { GiteeParams } from "./gitee-params";
import { GiteeUser } from "./gitee-user";

export class GiteeStorage<T extends BaseModel> extends BaseStorage<T> {

Expand All @@ -12,4 +18,32 @@ export class GiteeStorage<T extends BaseModel> extends BaseStorage<T> {
async find(params?: GiteeParams): Promise<T[]> {
return super.find(params);
}

/**
* Retrieves the detailed information of an issue from Gitee.
* @returns A promise that resolves to an object containing the issue details.
*/
async detail(): Promise<IssueDetail> {
const url = this.getRoute(RouteType.detail);
const response = await this.request.get<GiteeDetail>(url);
const {id, number, comments, created_at, updated_at} = response;
const result: IssueDetail = {
id: id,
issue_number: number,
comments: comments,
created_at: created_at,
updated_at: updated_at
};
return result;
}

protected extractUser(comment: BaseComment): User | null {
const { user } = comment;
if (user) {
const { id, name, avatar_url } = user as GiteeUser;
return { id, name, avatar_url };
}
return null;
}

}
50 changes: 50 additions & 0 deletions src/storage-lib/github/github-detail.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
export type GithubDetail = {
url: string,
repository_url: string,
labels_url: string,
comments_url: string,
events_url: string,
html_url: string,
id: number,
node_id: string,
number: number,
title: string,
user: {
login: string,
id: number,
node_id: string,
avatar_url: string,
gravatar_id: string,
url: string,
html_url: string,
followers_url: string,
following_url: string,
gists_url: string,
starred_url: string,
subscriptions_url: string,
organizations_url: string,
repos_url: string,
events_url: string,
received_events_url: string,
type: string,
site_admin: boolean
},
labels: [],
state: string,
locked: boolean,
assignee: string,
assignees: [],
milestone: string,
comments: number,
created_at: string,
updated_at: string,
closed_at: string,
author_association: string,
active_lock_reason: string,
body: string,
closed_by: string,
reactions: object,
timeline_url: string,
performed_via_github_app: string,
state_reason: string
}
33 changes: 33 additions & 0 deletions src/storage-lib/github/github-storage.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { BaseRequest } from "../../request-lib";
import { BaseComment } from "../base/base-comment";
import { BaseModel } from "../base/base-model";
import { BaseStorage } from "../base/base-storage";
import { IssueDetail } from "../base/issue-detail";
import { RouteType } from "../base/route-type";
import { User } from "../base/user";
import { GithubDetail } from "./github-detail";
import { GithubParams } from "./github-params";
import { GithubUser } from "./github-user";

export class GithubStorage<T extends BaseModel> extends BaseStorage<T> {

Expand All @@ -12,4 +18,31 @@ export class GithubStorage<T extends BaseModel> extends BaseStorage<T> {
async find(params?: GithubParams): Promise<T[]> {
return super.find(params);
}

/**
* Retrieves the detailed information of an issue from Gitee.
* @returns A promise that resolves to an object containing the issue details.
*/
async detail(): Promise<IssueDetail> {
const url = this.getRoute(RouteType.detail);
const response = await this.request.get<GithubDetail>(url);
const {id, number, comments, created_at, updated_at} = response;
const result: IssueDetail = {
id: id,
issue_number: number.toString(),
comments: comments,
created_at: created_at,
updated_at: updated_at
};
return result;
}

protected extractUser(comment: BaseComment): User | null {
const { user } = comment;
if (user) {
const { id, login, avatar_url } = user as GithubUser;
return { id, name: login, avatar_url };
}
return null;
}
}
Loading

0 comments on commit f64d236

Please sign in to comment.