-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from GuoXiCheng/dev-h-2
update file structure
- Loading branch information
Showing
24 changed files
with
237 additions
and
240 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}; | ||
|
6 changes: 3 additions & 3 deletions
6
src/request-lib/axios-request.ts → src/request-lib/axios/axios-request.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 32 additions & 32 deletions
64
src/request-lib/tiny-request.ts → src/request-lib/base/base-request.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
7 changes: 4 additions & 3 deletions
7
src/request-lib/wx-request.ts → src/request-lib/wx/wx-request.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export interface BaseStorage { | ||
findById(): void; | ||
find(): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Oops, something went wrong.