Skip to content

Commit

Permalink
Merge pull request #33 from GuoXiCheng/dev-c
Browse files Browse the repository at this point in the history
Dev c
  • Loading branch information
GuoXiCheng authored Dec 9, 2023
2 parents d7c6e48 + 6bc91bb commit 4d5685d
Show file tree
Hide file tree
Showing 14 changed files with 168 additions and 177 deletions.
8 changes: 4 additions & 4 deletions src/__tests__/helper/start-test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import axios from 'axios';
import 'dotenv/config';
import { RequestType } from '../../enums';
import { OfficialUrl, RequestType } from '../../enums';
import { createRequest } from '../../request-lib';
import { GiteeStorageOptions } from '../../storage-lib/interfaces';

Expand All @@ -14,7 +14,7 @@ export class StartTest {
requestType: RequestType.axios,
request: axios,
accessToken: process.env.TEST_GITEE_TOKEN as string,
baseUrl: "https://gitee.com",
baseUrl: OfficialUrl.gitee,
});
}

Expand All @@ -32,7 +32,7 @@ export class StartTest {
requestType: RequestType.axios,
request: axios,
accessToken: process.env.TEST_GITHUB_TOKEN as string,
baseUrl: "https://api.github.com",
baseUrl: OfficialUrl.github,
});
}

Expand All @@ -41,7 +41,7 @@ export class StartTest {
requestType: RequestType.axios,
request: axios,
accessToken: process.env.TEST_GITLAB_TOKEN as string,
baseUrl: "https://gitlab.com",
baseUrl: OfficialUrl.gitlab,
});
}
}
3 changes: 1 addition & 2 deletions src/__tests__/helper/user-storage.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { GiteeStorage } from '../../storage-lib/gitee-storage';
import { UserModel } from './user-model';
import { GiteeStorageOptions } from '../../storage-lib/interfaces';

export class UserStorage extends GiteeStorage<UserModel> {
export class UserStorage extends GiteeStorage {
constructor(options: GiteeStorageOptions) {
super(options);
}
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/user-storage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ import { UserStorage } from "./helper/user-storage";

test('Test Storage Lib', async () => {
const userStorage = new UserStorage(StartTest.getGiteeOptions());
const detail = await userStorage.findAll();
const detail = await userStorage.find();
expect(detail.length).toBeGreaterThan(0);
}, 30000);
6 changes: 6 additions & 0 deletions src/enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,10 @@ export enum StoragePlatform {
export enum RequestType {
axios = "axios",
wx = "wx"
}

export enum OfficialUrl {
gitee = "https://gitee.com",
github = "https://api.github.com",
gitlab = "https://gitlab.com"
}
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {createRequest} from "./request-lib";
export {createRequest};
import { OfficialUrl } from "./enums";
export {createRequest, OfficialUrl};

15 changes: 9 additions & 6 deletions src/request-lib/axios-request.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
import { AxiosInstance } from 'axios';
import { TinyRequest } from './tiny-request';
import { TinyRequestOptions } from './interfaces';

export class AxiosRequest extends TinyRequest {
private axios: AxiosInstance;
private accessToken: string;
constructor(
protected axios: AxiosInstance,
protected baseUrl: string,
protected accessToken: string
protected options: TinyRequestOptions
) {
super(axios, baseUrl, accessToken);
super(options);
this.axios = options.request as AxiosInstance;
this.accessToken = options.accessToken;
}

async get<T>(url: string): Promise<T> {
return new Promise((resolve, reject) => {
this.axios.get(url, {
this.axios.get<T>(url, {
headers: {
'Authorization': `Bearer ${this.accessToken}`
}
}).then((res) => {
resolve(res.data as T);
resolve(res.data);
}).catch(error => {
reject(error);
});
Expand Down
19 changes: 8 additions & 11 deletions src/request-lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,27 @@
import { AxiosInstance } from "axios";
import { AxiosRequestFactory, WxRequestFactory } from "./request-factories";
import { TinyRequestOptions, WxInstance } from "./interfaces";
import { RequestType } from "../enums";
import { AxiosRequest } from "./axios-request";
import { WxRequest } from "./wx-request";

/**
* Creates a request based on the provided options.
* @param options - The options for creating the request.
* @returns The created request.
* @throws Error if the request lib type is invalid.
*/
export function createRequest<T extends RequestType>(options: TinyRequestOptions<T>) {
const { requestType, request, accessToken, baseUrl } = options;

export function createRequest(options: TinyRequestOptions) {
const { requestType, request } = options;
switch (requestType) {
case RequestType.axios:
if (isAxiosInstance(request)) {
return new AxiosRequestFactory().createRequest(request, baseUrl, accessToken);
return new AxiosRequest(options);
}
case RequestType.wx:
if (isWxInstance(request)) {
return new WxRequestFactory().createRequest(request, baseUrl, accessToken);
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'
Expand Down
126 changes: 10 additions & 116 deletions src/request-lib/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { AxiosInstance } from "axios";
import { RequestType, StoragePlatform } from "../enums";
import { GiteeUser, GithubUser, GitlabUser } from "./user";

export interface WxInstance {
request(options: WxRequestOptions): void;
Expand All @@ -14,21 +15,21 @@ export interface WxRequestOptions {
fail: (errMsg: string, errNo: number) => void;
}

// export interface TinyRequest {
// get<T>(url: string): Promise<T>;
// post(url: string): void;
// ping<P extends keyof typeof StoragePlatform>(platform: P): Promise<StoragePlatformUserMap[typeof StoragePlatform[P]]>;
// };

export type RequestInstance = WxInstance | AxiosInstance;

export const OfficialUrlValues = {
gitee: "https://gitee.com",
github: "https://api.github.com",
gitlab: "https://gitlab.com"
} as const;

type RequestTypeMap = {
[RequestType.wx]: WxInstance;
[RequestType.axios]: AxiosInstance;
}
export interface TinyRequestOptions<T extends RequestType> {
requestType: T;
request: RequestTypeMap[T];
export interface TinyRequestOptions {
requestType: keyof typeof RequestType;
request: RequestInstance;
baseUrl: string;
accessToken: string;
}
Expand All @@ -39,112 +40,5 @@ export type StoragePlatformUserMap = {
[StoragePlatform.gitlab]: GitlabUser;
}

export interface GiteeUser {
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;
blog: string | null;
weibo: string | null;
bio: string;
public_repos: number;
public_gists: number;
followers: number;
following: number;
stared: number;
watched: number;
created_at: string;
updated_at: string;
email: string | null;
}

export interface GithubUser {
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;
name: string;
company: string | null;
blog: string;
location: string | null;
email: string | null;
hireable: boolean | null;
bio: string;
twitter_username: string | null;
public_repos: number;
public_gists: number;
followers: number;
following: number;
created_at: string;
updated_at: string;
}

export interface GitlabUser {
id: number;
username: string;
name: string;
state: string;
locked: boolean;
avatar_url: string;
web_url: string;
created_at: string;
bio: string;
location: string;
public_email: string | null;
skype: string;
linkedin: string;
twitter: string;
discord: string;
website_url: string;
organization: string;
job_title: string;
pronouns: string | null;
bot: boolean;
work_information: any | null;
local_time: string | null;
last_sign_in_at: string | null;
confirmed_at: string;
last_activity_on: string;
email: string;
theme_id: number;
color_scheme_id: number;
projects_limit: number;
current_sign_in_at: string | null;
identities: any[];
can_create_group: boolean;
can_create_project: boolean;
two_factor_enabled: boolean;
external: boolean;
private_profile: boolean;
commit_email: string;
shared_runners_minutes_limit: number | null;
extra_shared_runners_minutes_limit: number | null;
scim_identities: any[];
}
21 changes: 0 additions & 21 deletions src/request-lib/request-factories.ts

This file was deleted.

11 changes: 6 additions & 5 deletions src/request-lib/tiny-request.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { StoragePlatform } from "../enums";
import { RequestInstance, StoragePlatformUserMap } from "./interfaces";
import { RequestInstance, StoragePlatformUserMap, TinyRequestOptions } from "./interfaces";

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

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

0 comments on commit 4d5685d

Please sign in to comment.