Skip to content

Commit

Permalink
Merge pull request #64 from GuoXiCheng/dev-c
Browse files Browse the repository at this point in the history
Dev c
  • Loading branch information
GuoXiCheng authored Jan 4, 2024
2 parents e6e868b + 2a8314c commit e52ea3d
Show file tree
Hide file tree
Showing 15 changed files with 66 additions and 69 deletions.
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.18",
"version": "1.0.19",
"description": "",
"main": "dist/bundle.cjs.js",
"module": "dist/bundle.esm.js",
Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/helper/book-storage.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { GithubStorage } from "../../storage-lib";
import { SingletonFactory } from "../../utils";
import { BookModel } from "./book-model";
import { GITHUB_NUMBER, githubRequest } from "./helper";
import { githubRequest } from "./helper";

class BookStorage extends GithubStorage<BookModel> {
constructor() {
super(githubRequest, GITHUB_NUMBER);
super(githubRequest);
}
}

Expand Down
15 changes: 8 additions & 7 deletions src/__tests__/helper/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ export const GITLAB_NUMBER = process.env.TEST_GITLAB_NUMBER as string;
export const ENCRYPT_KEY = "MySecretPassphrase";

export const giteeRequest = createRequest({
requestType: 'axios',
request: axios,
httpLib: 'axios',
httpClient: axios,
accessToken: process.env.TEST_GITEE_TOKEN as string,
platform: 'gitee',
owner: process.env.TEST_GITEE_OWNER as string,
Expand All @@ -37,17 +37,18 @@ export const giteeRequest = createRequest({
});

export const githubRequest = createRequest({
requestType: 'axios',
request: axios,
httpLib: 'axios',
httpClient: axios,
accessToken: process.env.TEST_GITHUB_TOKEN as string,
platform: 'github',
owner: process.env.TEST_GITHUB_OWNER as string,
repo: process.env.TEST_GITHUB_REPO as string
repo: process.env.TEST_GITHUB_REPO as string,
issueNumber: process.env.TEST_GITHUB_NUMBER as string
});

export const gitlabRequest = createRequest({
requestType: 'axios',
request: axios,
httpLib: 'axios',
httpClient: axios,
accessToken: process.env.TEST_GITLAB_TOKEN as string,
platform: 'gitlab',
projectId: process.env.TEST_GITLAB_PROJECT_ID as string
Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/wx-request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ const wx = {
};

const request = createRequest({
requestType: 'wx',
request: wx,
httpLib: 'wx',
httpClient: wx,
platform: 'gitee',
owner: 'test-owner',
repo: 'test-repo',
Expand Down
4 changes: 2 additions & 2 deletions src/request-lib/axios/axios-options.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AxiosInstance } from "axios";

export type AxiosOptions = {
requestType: 'axios';
request: AxiosInstance;
httpLib: 'axios';
httpClient: AxiosInstance;
}
30 changes: 13 additions & 17 deletions src/request-lib/axios/axios-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,20 @@ export class AxiosRequest extends BaseRequest {
protected options: RequestOptions
) {
super(options);
this.axios = options.request as AxiosInstance;
this.axios = options.httpClient as AxiosInstance;
}

async sendRequest<T>(method: RequestMethods, url: string, body?: string, params?: any): Promise<T> {
try {
const response = await this.axios.request<T>({
url,
method,
headers: {
'Authorization': `Bearer ${this.accessToken}`
},
...(body && { data: { body } }),
...(params && { params })
});

return response.data;
} catch (error: any) {
throw error;
}
protected async sendRequest<T>(method: RequestMethods, url: string, body?: string, params?: any): Promise<T> {
const response = await this.axios.request<T>({
url,
method,
headers: {
'Authorization': `Bearer ${this.accessToken}`
},
...(body && { data: { body } }),
...(params && { params })
});

return response.data;
}
}
3 changes: 3 additions & 0 deletions src/request-lib/base/base-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export abstract class BaseRequest {
public readonly useEncrypt: boolean;
public readonly encryptFn?: (data: string) => string;
public readonly decryptFn?: (data: string) => string;
public readonly issueNumber?: string;
constructor(
protected options: RequestOptions
) {
Expand All @@ -19,6 +20,8 @@ export abstract class BaseRequest {
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
1 change: 1 addition & 0 deletions src/request-lib/base/request-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { WxOptions } from "../wx/wx-options";

export type RequestOptions = {
baseUrl?: string;
issueNumber?: string;
accessToken: string;
}
& (GiteeOptions | GithubOptions | GitlabOptions)
Expand Down
8 changes: 4 additions & 4 deletions src/request-lib/create-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import { RequestOptions } from "./base/request-options";


export function createRequest(options: RequestOptions) {
const { requestType, request } = options;
switch (requestType) {
const { httpLib, httpClient } = options;
switch (httpLib) {
case RequestType.axios:
if (isAxiosInstance(request)) {
if (isAxiosInstance(httpClient)) {
return new AxiosRequest(options);
}
case RequestType.wx:
if (isWxInstance(request)) {
if (isWxInstance(httpClient)) {
return new WxRequest(options);
}
default:
Expand Down
4 changes: 2 additions & 2 deletions src/request-lib/wx/wx-options.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { WxInstance } from "./wx-interface";

export type WxOptions = {
requestType: 'wx';
request: WxInstance
httpLib: 'wx';
httpClient: WxInstance
}
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 @@ -9,7 +9,7 @@ export class WxRequest extends BaseRequest {
protected options: RequestOptions
) {
super(options);
this.wx = options.request as WxInstance;
this.wx = options.httpClient as WxInstance;
}

protected async sendRequest<T>(method: RequestMethods, url: string, body?: string, params?: any): Promise<T> {
Expand Down
52 changes: 24 additions & 28 deletions src/storage-lib/base/base-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,21 @@ export abstract class BaseStorage<T extends BaseModel> {
public readonly decryptFn?: (data: string) => string;

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

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

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

protected abstract extractUser(comment: BaseComment): User | null;
Expand Down Expand Up @@ -85,7 +94,7 @@ export abstract class BaseStorage<T extends BaseModel> {
if (order === false) {
return Promise.all(data.map((item) => this.create(item)));
}

const result: T[] = [];
for (const item of data) {
result.push(await this.create(item));
Expand Down Expand Up @@ -127,34 +136,21 @@ export abstract class BaseStorage<T extends BaseModel> {
}

// 序列化: 将对象转换为字符串
serialize<T>(obj: T): string {
try {
if (this.useEncrypt && this.encryptFn) {
return this.encryptFn(JSON.stringify(obj));
}
return JSON.stringify(obj);
} catch (error) {
console.error(error);
throw new Error(`can not serialize ${obj}`);
}
protected serialize<T>(obj: T): string {
return (this.useEncrypt && this.encryptFn)
? this.encryptFn(JSON.stringify(obj))
: JSON.stringify(obj);
}

// 反序列化: 将字符串转换为对象
deserialize<T>(comment: BaseComment): T {
try {
const {id, body, created_at, updated_at} = comment;
let obj;
if (this.useEncrypt && this.decryptFn) {
const decryptedBody = this.decryptFn(body);
obj = JSON.parse(decryptedBody);
} else {
obj = JSON.parse(body);
}
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}`);
}
protected deserialize<T>(comment: BaseComment): T {
const { id, body, created_at, updated_at } = comment;

const parsedBody = this.useEncrypt && this.decryptFn
? JSON.parse(this.decryptFn(body))
: JSON.parse(body);
const user = this.extractUser(comment);

return { id, ...parsedBody, created_at, updated_at, user };
}
}
2 changes: 1 addition & 1 deletion src/storage-lib/gitee/gitee-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { GiteeUser } from "./gitee-user";

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

constructor(protected request: BaseRequest, protected issueNumber: string) {
constructor(protected request: BaseRequest, issueNumber?: string) {
super(request, issueNumber);
}

Expand Down
2 changes: 1 addition & 1 deletion src/storage-lib/github/github-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { GithubUser } from "./github-user";

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

constructor(protected request: BaseRequest, protected issueNumber: string) {
constructor(protected request: BaseRequest, issueNumber?: string) {
super(request, issueNumber);
}

Expand Down
2 changes: 1 addition & 1 deletion src/storage-lib/gitlab/gitlab-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { GitlabUser } from "./gitlab-user";

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

constructor(protected request: BaseRequest, protected issueNumber: string) {
constructor(protected request: BaseRequest, issueNumber?: string) {
super(request, issueNumber);
}

Expand Down

0 comments on commit e52ea3d

Please sign in to comment.