Skip to content

Commit

Permalink
Merge pull request #67 from GuoXiCheng/main
Browse files Browse the repository at this point in the history
publish 1.0.20
  • Loading branch information
GuoXiCheng authored Jan 5, 2024
2 parents f22e347 + fa20e19 commit f4f8cc7
Show file tree
Hide file tree
Showing 17 changed files with 83 additions and 60 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ jobs:
TEST_GITLAB_TOKEN: ${{ secrets.TEST_GITLAB_TOKEN }}
TEST_GITLAB_PROJECT_ID: ${{ secrets.TEST_GITLAB_PROJECT_ID }}
TEST_GITLAB_NUMBER: ${{ secrets.TEST_GITLAB_NUMBER }}
TEST_ENCRYPT_KEY: ${{ secrets.TEST_ENCRYPT_KEY }}


- name: Upload coverage reports to Codecov
Expand Down
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.19",
"version": "1.0.20",
"description": "",
"main": "dist/bundle.cjs.js",
"module": "dist/bundle.esm.js",
Expand Down
5 changes: 4 additions & 1 deletion src/__tests__/book-storage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,10 @@ describe('Test Book Storage', () => {
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();
expect(findByIdResult.created_by).not.toBeNull();
expect(typeof findByIdResult.created_by.user_id).toBe('number');
expect(typeof findByIdResult.created_by.username).toBe('string');
expect(typeof findByIdResult.created_by.avatar_url).toBe('string');
});

test('Test updateById Book', async () => {
Expand Down
5 changes: 4 additions & 1 deletion src/__tests__/chat-storage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,10 @@ describe('Use Gitlab Test Chat Storage', () => {

const findByIdResult = await Chat.findById(findResult[0].id);
expect(findByIdResult).toEqual(findResult[0]);
expect(findByIdResult.user).not.toBeNull();
expect(findByIdResult.created_by).not.toBeNull();
expect(typeof findByIdResult.created_by.user_id).toBe('number');
expect(typeof findByIdResult.created_by.username).toBe('string');
expect(typeof findByIdResult.created_by.avatar_url).toBe('string');
});

test('Test updateById Chat', async () => {
Expand Down
6 changes: 3 additions & 3 deletions src/__tests__/helper/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const GITHUB_NUMBER = process.env.TEST_GITHUB_NUMBER as string;

export const GITLAB_NUMBER = process.env.TEST_GITLAB_NUMBER as string;

export const ENCRYPT_KEY = "MySecretPassphrase";
export const TEST_ENCRYPT_KEY = process.env.TEST_ENCRYPT_KEY as string;

export const giteeRequest = createRequest({
httpLib: 'axios',
Expand All @@ -29,10 +29,10 @@ export const giteeRequest = createRequest({
repo: process.env.TEST_GITEE_REPO as string,
useEncrypt: true,
encryptFn: (data: string) => {
return CryptoJS.AES.encrypt(data, ENCRYPT_KEY).toString();
return CryptoJS.AES.encrypt(data, TEST_ENCRYPT_KEY).toString();
},
decryptFn: (data: string) => {
return CryptoJS.AES.decrypt(data, ENCRYPT_KEY).toString(CryptoJS.enc.Utf8);
return CryptoJS.AES.decrypt(data, TEST_ENCRYPT_KEY).toString(CryptoJS.enc.Utf8);
}
});

Expand Down
5 changes: 4 additions & 1 deletion src/__tests__/user-storage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@ describe('Test User Storage', () => {

const findByIdResult = await User.findById(findResult[0].id);
expect(findByIdResult).toEqual(findResult[0]);
expect(findByIdResult.user).not.toBeNull();
expect(findByIdResult.created_by).not.toBeNull();
expect(typeof findByIdResult.created_by.user_id).toBe('number');
expect(typeof findByIdResult.created_by.username).toBe('string');
expect(typeof findByIdResult.created_by.avatar_url).toBe('string');
});

test('Test updateById User', async () => {
Expand Down
2 changes: 1 addition & 1 deletion src/request-lib/axios/axios-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { RequestMethods } from '../base/request-methods';
export class AxiosRequest extends BaseRequest {
private axios: AxiosInstance;
constructor(
protected options: RequestOptions
public options: RequestOptions
) {
super(options);
this.axios = options.httpClient as AxiosInstance;
Expand Down
28 changes: 9 additions & 19 deletions src/request-lib/base/base-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,14 @@ import { RequestMethods } from "./request-methods";
import { RequestOptions } from "./request-options";

export abstract class BaseRequest {
protected readonly baseUrl: string;
protected readonly baseURL: string;
protected readonly accessToken: string;

public readonly useEncrypt: boolean;
public readonly encryptFn?: (data: string) => string;
public readonly decryptFn?: (data: string) => string;
public readonly issueNumber?: string;
constructor(
protected options: RequestOptions
public options: RequestOptions
) {
this.baseUrl = options.baseUrl ? options.baseUrl : this.getBaseUrl();
this.baseURL = options.baseURL ? options.baseURL : this.getBaseUrl();
this.accessToken = options.accessToken;

this.useEncrypt = options.useEncrypt || false;
this.encryptFn = options.encryptFn;
this.decryptFn = options.decryptFn;

this.issueNumber = options.issueNumber;
}

protected abstract sendRequest<T>(method: RequestMethods, url: string, body?: string, params?: any): Promise<T>;
Expand Down Expand Up @@ -49,11 +39,11 @@ export abstract class BaseRequest {
async authenticate() {
switch(this.options.platform) {
case StoragePlatform.gitee:
return this.get<GiteeUser>(`${this.baseUrl}/api/v5/user`);
return this.get<GiteeUser>(`${this.baseURL}/api/v5/user`);
case StoragePlatform.github:
return this.get<GithubUser>(`${this.baseUrl}/user`);
return this.get<GithubUser>(`${this.baseURL}/user`);
case StoragePlatform.gitlab:
return this.get<GitlabUser>(`${this.baseUrl}/api/v4/user`);
return this.get<GitlabUser>(`${this.baseURL}/api/v4/user`);
default:
throw new Error('Unsupported Platform');
}
Expand All @@ -75,11 +65,11 @@ export abstract class BaseRequest {
getEndpoint() {
switch(this.options.platform) {
case StoragePlatform.gitee:
return `${this.baseUrl}/api/v5/repos/${this.options.owner}/${this.options.repo}`;
return `${this.baseURL}/api/v5/repos/${this.options.owner}/${this.options.repo}`;
case StoragePlatform.github:
return `${this.baseUrl}/repos/${this.options.owner}/${this.options.repo}`;
return `${this.baseURL}/repos/${this.options.owner}/${this.options.repo}`;
case StoragePlatform.gitlab:
return `${this.baseUrl}/api/v4/projects/${this.options.projectId}`;
return `${this.baseURL}/api/v4/projects/${this.options.projectId}`;
default:
throw new Error('Unsupported Platform');
}
Expand Down
2 changes: 1 addition & 1 deletion src/request-lib/base/request-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { AxiosOptions } from "../axios/axios-options";
import { WxOptions } from "../wx/wx-options";

export type RequestOptions = {
baseUrl?: string;
baseURL?: string;
issueNumber?: string;
accessToken: string;
}
Expand Down
2 changes: 1 addition & 1 deletion src/request-lib/wx/wx-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { WxInstance, WxRequestOptions } from './wx-interface';
export class WxRequest extends BaseRequest {
private wx: WxInstance;
constructor(
protected options: RequestOptions
public options: RequestOptions
) {
super(options);
this.wx = options.httpClient as WxInstance;
Expand Down
5 changes: 5 additions & 0 deletions src/storage-lib/base/author.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export type Author = {
user_id: number;
username: string;
avatar_url: string;
}
7 changes: 3 additions & 4 deletions src/storage-lib/base/base-model.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { User } from "./user";

import { Author } from "./author";
export interface BaseModel {
id: number;
created_at: string;
updated_at: string;
user: User;
created_at: string;
created_by: Author;
}
35 changes: 22 additions & 13 deletions src/storage-lib/base/base-storage.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,36 @@
import { BaseRequest } from "../../request-lib";
import { Author } from "./author";
import { BaseComment } from "./base-comment";
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;
public readonly encryptFn?: (data: string) => string;
public readonly decryptFn?: (data: string) => string;

protected endpoint: string;
protected issueNumber: string;
protected readonly useEncrypt: boolean;
protected readonly encryptFn?: (data: string) => string;
protected readonly decryptFn?: (data: string) => string;

constructor(protected request: BaseRequest, issueNumber?: string) {
this.endpoint = request.getEndpoint();

this.useEncrypt = request.useEncrypt || false;
this.encryptFn = request.encryptFn;
this.decryptFn = request.decryptFn;
const { useEncrypt, encryptFn, decryptFn } = request.options;
this.useEncrypt = useEncrypt || false;
this.encryptFn = encryptFn;
this.decryptFn = decryptFn;

if (request.issueNumber != null) {
this.issueNumber = request.issueNumber;
if (request.options.issueNumber != null) {
this.issueNumber = request.options.issueNumber;
} else if (issueNumber != null) {
this.issueNumber = issueNumber;
} else {
throw new Error('issueNumber is required');
}
}

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

protected getRoute(routeType: keyof typeof RouteType, id?: number): string {
switch (routeType) {
Expand Down Expand Up @@ -149,8 +150,16 @@ export abstract class BaseStorage<T extends BaseModel> {
const parsedBody = this.useEncrypt && this.decryptFn
? JSON.parse(this.decryptFn(body))
: JSON.parse(body);
const user = this.extractUser(comment);
const created_by = this.extractUser(comment);

const result: T = {
id,
...parsedBody,
updated_at,
created_at,
created_by
}

return { id, ...parsedBody, created_at, updated_at, user };
return result;
}
}
5 changes: 0 additions & 5 deletions src/storage-lib/base/user.ts

This file was deleted.

11 changes: 8 additions & 3 deletions src/storage-lib/gitee/gitee-storage.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { BaseRequest } from "../../request-lib";
import { Author } from "../base/author";
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";
Expand Down Expand Up @@ -37,11 +37,16 @@ export class GiteeStorage<T extends BaseModel> extends BaseStorage<T> {
return result;
}

protected extractUser(comment: BaseComment): User | null {
protected extractUser(comment: BaseComment): Author | null {
const { user } = comment;
if (user) {
const { id, name, avatar_url } = user as GiteeUser;
return { id, name, avatar_url };
const author: Author = {
user_id: id,
username: name,
avatar_url: avatar_url
};
return author;
}
return null;
}
Expand Down
11 changes: 8 additions & 3 deletions src/storage-lib/github/github-storage.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { BaseRequest } from "../../request-lib";
import { Author } from "../base/author";
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";
Expand Down Expand Up @@ -37,11 +37,16 @@ export class GithubStorage<T extends BaseModel> extends BaseStorage<T> {
return result;
}

protected extractUser(comment: BaseComment): User | null {
protected extractUser(comment: BaseComment): Author | null {
const { user } = comment;
if (user) {
const { id, login, avatar_url } = user as GithubUser;
return { id, name: login, avatar_url };
const author: Author = {
user_id: id,
username: login,
avatar_url: avatar_url
};
return author;
}
return null;
}
Expand Down
11 changes: 8 additions & 3 deletions src/storage-lib/gitlab/gitlab-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { BaseStorage } from "../base/base-storage";
import { IssueDetail } from "../base/issue-detail";
import { PlainObject } from "../base/plain-object";
import { RouteType } from "../base/route-type";
import { User } from "../base/user";
import { Author } from "../base/author";
import { GitlabDetail } from "./gitlab-detail";
import { GitlabParams } from "./gitlab-params";
import { GitlabUser } from "./gitlab-user";
Expand Down Expand Up @@ -85,11 +85,16 @@ export class GitlabStorage<T extends BaseModel> extends BaseStorage<T> {
return result;
}

protected extractUser(comment: BaseComment): User | null {
protected extractUser(comment: BaseComment): Author | null {
const { author } = comment;
if (author) {
const { id, name, avatar_url } = author as GitlabUser;
return { id, name, avatar_url };
const result: Author = {
user_id: id,
username: name,
avatar_url: avatar_url
};
return result;
}
return null;
}
Expand Down

0 comments on commit f4f8cc7

Please sign in to comment.