Skip to content

Commit

Permalink
Merge pull request #34 from GuoXiCheng/dev-h-2
Browse files Browse the repository at this point in the history
update file structure
  • Loading branch information
GuoXiCheng authored Dec 10, 2023
2 parents 4d5685d + 8330d25 commit 70f7159
Show file tree
Hide file tree
Showing 24 changed files with 237 additions and 240 deletions.
5 changes: 2 additions & 3 deletions src/__tests__/helper/start-test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import axios from 'axios';
import 'dotenv/config';
import { OfficialUrl, RequestType } from '../../enums';
import { createRequest } from '../../request-lib';
import { GiteeStorageOptions } from '../../storage-lib/interfaces';

import { GiteeStorageOptions } from '../../storage-lib/gitee/gitee-storage-options';
import {createRequest} from '../../request-lib';
export class StartTest {
constructor() {

Expand Down
5 changes: 2 additions & 3 deletions src/__tests__/helper/user-model.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { TinyModel } from "../../storage-lib/interfaces";

export class UserModel extends TinyModel {
import { BaseModel } from '../../storage-lib/base/base-model';
export class UserModel extends BaseModel {
name: string;
age: number;
}
4 changes: 2 additions & 2 deletions src/__tests__/helper/user-storage.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { GiteeStorage } from '../../storage-lib/gitee-storage';
import { GiteeStorageOptions } from '../../storage-lib/interfaces';
import { GiteeStorage } from '../../storage-lib/gitee/gitee-storage';
import { GiteeStorageOptions } from '../../storage-lib/gitee/gitee-storage-options';

export class UserStorage extends GiteeStorage {
constructor(options: GiteeStorageOptions) {
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {createRequest} from "./request-lib";
import {createRequest} from "./request-lib/create-request";
import { OfficialUrl } from "./enums";
export {createRequest, OfficialUrl};

Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { AxiosInstance } from 'axios';
import { TinyRequest } from './tiny-request';
import { TinyRequestOptions } from './interfaces';
import { TinyRequestOptions } from '../interfaces';
import { BaseRequest } from '../base/base-request';

export class AxiosRequest extends TinyRequest {
export class AxiosRequest extends BaseRequest {
private axios: AxiosInstance;
private accessToken: string;
constructor(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
import { StoragePlatform } from "../enums";
import { RequestInstance, StoragePlatformUserMap, TinyRequestOptions } from "./interfaces";

export abstract class TinyRequest {
private baseUrl: string;
constructor(
protected options: TinyRequestOptions
) {
this.baseUrl = options.baseUrl;
}

abstract get<T>(url: string): Promise<T>;
abstract post(url: string): void;

async ping<P extends keyof typeof StoragePlatform>(platform: P): Promise<StoragePlatformUserMap[typeof StoragePlatform[P]]> {
let url: string;
switch (platform) {
case StoragePlatform.gitee:
url = `${this.baseUrl}/api/v5/user`;
break;
case StoragePlatform.github:
url = `${this.baseUrl}/user`;
break;
case StoragePlatform.gitlab:
url = `${this.baseUrl}/api/v3/user`;
break;
default:
throw new Error('Unsupported platform');
}

return this.get<StoragePlatformUserMap[typeof StoragePlatform[P]]>(url);
}
import { StoragePlatform } from "../../enums";
import { StoragePlatformUserMap, TinyRequestOptions } from "../interfaces";

export abstract class BaseRequest {
private baseUrl: string;
constructor(
protected options: TinyRequestOptions
) {
this.baseUrl = options.baseUrl;
}

abstract get<T>(url: string): Promise<T>;
abstract post(url: string): void;

async ping<P extends keyof typeof StoragePlatform>(platform: P): Promise<StoragePlatformUserMap[typeof StoragePlatform[P]]> {
let url: string;
switch (platform) {
case StoragePlatform.gitee:
url = `${this.baseUrl}/api/v5/user`;
break;
case StoragePlatform.github:
url = `${this.baseUrl}/user`;
break;
case StoragePlatform.gitlab:
url = `${this.baseUrl}/api/v3/user`;
break;
default:
throw new Error('Unsupported platform');
}

return this.get<StoragePlatformUserMap[typeof StoragePlatform[P]]>(url);
}
}
37 changes: 37 additions & 0 deletions src/request-lib/create-request.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { AxiosInstance } from "axios";
import { TinyRequestOptions } from "./interfaces";
import { RequestType } from "../enums";
import { AxiosRequest } from "./axios/axios-request";
import { WxRequest } from "./wx/wx-request";
import { WxInstance } from "./wx/wx-interface";


export function createRequest(options: TinyRequestOptions) {
const { requestType, request } = options;
switch (requestType) {
case RequestType.axios:
if (isAxiosInstance(request)) {
return new AxiosRequest(options);
}
case RequestType.wx:
if (isWxInstance(request)) {
return new WxRequest(options);
}
default:
throw new Error('invalid request lib type');
}
}


function isAxiosInstance(instance: any): instance is AxiosInstance {
return instance
&& typeof instance.get === 'function'
&& typeof instance.post === 'function'
&& typeof instance.put === 'function'
&& typeof instance.delete === 'function'
&& typeof instance.patch === 'function';
}

function isWxInstance(instance: any): instance is WxInstance {
return instance && typeof instance.request === 'function';
}
37 changes: 1 addition & 36 deletions src/request-lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1 @@
import { AxiosInstance } from "axios";
import { TinyRequestOptions, WxInstance } from "./interfaces";
import { RequestType } from "../enums";
import { AxiosRequest } from "./axios-request";
import { WxRequest } from "./wx-request";


export function createRequest(options: TinyRequestOptions) {
const { requestType, request } = options;
switch (requestType) {
case RequestType.axios:
if (isAxiosInstance(request)) {
return new AxiosRequest(options);
}
case RequestType.wx:
if (isWxInstance(request)) {
return new WxRequest(options);
}
default:
throw new Error('invalid request lib type');
}
}


function isAxiosInstance(instance: any): instance is AxiosInstance {
return instance
&& typeof instance.get === 'function'
&& typeof instance.post === 'function'
&& typeof instance.put === 'function'
&& typeof instance.delete === 'function'
&& typeof instance.patch === 'function';
}

function isWxInstance(instance: any): instance is WxInstance {
return instance && typeof instance.request === 'function';
}
export {createRequest} from './create-request';
20 changes: 2 additions & 18 deletions src/request-lib/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,7 @@
import { AxiosInstance } from "axios";
import { RequestType, StoragePlatform } from "../enums";
import { GiteeUser, GithubUser, GitlabUser } from "./user";

export interface WxInstance {
request(options: WxRequestOptions): void;
[prop: string]: any;
}

export interface WxRequestOptions {
url: string;
method: 'GET' | 'POST';
header?: object;
success: (res: { data: string | Object | ArrayBuffer, statusCode: number }) => void;
fail: (errMsg: string, errNo: number) => void;
}
import { WxInstance } from "./wx/wx-interface";
import { GiteeUser, GithubUser, GitlabUser } from "../storage-lib";

export type RequestInstance = WxInstance | AxiosInstance;

Expand All @@ -23,10 +11,6 @@ export const OfficialUrlValues = {
gitlab: "https://gitlab.com"
} as const;

type RequestTypeMap = {
[RequestType.wx]: WxInstance;
[RequestType.axios]: AxiosInstance;
}
export interface TinyRequestOptions {
requestType: keyof typeof RequestType;
request: RequestInstance;
Expand Down
109 changes: 0 additions & 109 deletions src/request-lib/user.ts

This file was deleted.

12 changes: 12 additions & 0 deletions src/request-lib/wx/wx-interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export interface WxInstance {
request(options: WxRequestOptions): void;
[prop: string]: any;
}

export interface WxRequestOptions {
url: string;
method: 'GET' | 'POST';
header?: object;
success: (res: { data: string | Object | ArrayBuffer, statusCode: number }) => void;
fail: (errMsg: string, errNo: number) => void;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { TinyRequestOptions, WxInstance } from './interfaces';
import { TinyRequest } from './tiny-request';
import { BaseRequest } from '../base/base-request';
import { TinyRequestOptions } from '../interfaces';
import { WxInstance } from './wx-interface';

export class WxRequest extends TinyRequest {
export class WxRequest extends BaseRequest {
private wx: WxInstance;
private accessToken: string;
constructor(
Expand Down
5 changes: 5 additions & 0 deletions src/storage-lib/base/base-model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export class BaseModel {
id!: number;
created_at!: string;
updated_at!: string;
}
4 changes: 4 additions & 0 deletions src/storage-lib/base/base-storage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface BaseStorage {
findById(): void;
find(): void;
}
7 changes: 7 additions & 0 deletions src/storage-lib/gitee/gitee-response.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export interface GiteeResponse {
id: number;
body: string;
user: object;
created_at: string;
updated_at: string;
}
9 changes: 9 additions & 0 deletions src/storage-lib/gitee/gitee-storage-options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { BaseRequest } from "../../request-lib/base/base-request";

export interface GiteeStorageOptions {
request: BaseRequest;
owner: string;
repo: string;
number: string;
baseUrl?: string;
}
Loading

0 comments on commit 70f7159

Please sign in to comment.