Skip to content

Commit

Permalink
Merge pull request #35 from GuoXiCheng/dev-c
Browse files Browse the repository at this point in the history
Dev c
  • Loading branch information
GuoXiCheng authored Dec 11, 2023
2 parents 70f7159 + fe6d3af commit 1b5dafb
Show file tree
Hide file tree
Showing 19 changed files with 129 additions and 103 deletions.
27 changes: 14 additions & 13 deletions src/__tests__/ping.test.ts → src/__tests__/authenticate.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { StartTest } from "./helper/start-test";

describe('Test Ping Function', () => {
test('Test Ping Gitee', async () => {
const request = StartTest.getGiteeRequest();
const res = await request.ping("gitee");
describe('Test Authenticate Function', () => {
test('Test Authenticate Gitee', async () => {
const request = StartTest.createGiteeRequest();
const res = await request.authenticate();
expect(Object.keys(res)).toEqual([
'id', 'login',
'name', 'avatar_url',
Expand All @@ -20,11 +20,12 @@ describe('Test Ping Function', () => {
'stared', 'watched',
'created_at', 'updated_at',
'email'
]);
])
});
test('Test Ping Github', async () => {
const request = StartTest.getGithubRequest();
const res = await request.ping("github");

test('Test Authenticate Github', async () => {
const request = StartTest.createGithubRequest();
const res = await request.authenticate();
expect(Object.keys(res)).toEqual([
'login', 'id',
'node_id', 'avatar_url',
Expand All @@ -44,9 +45,10 @@ describe('Test Ping Function', () => {
'created_at', 'updated_at'
]);
});
test('Test Ping Gitlab', async () => {
const request = StartTest.getGitlabRequest();
const res = await request.ping("gitlab");

test('Test Authenticate Gitlab', async () => {
const request = StartTest.createGitlabRequest();
const res = await request.authenticate();
expect(Object.keys(res)).toEqual([
'id',
'username',
Expand Down Expand Up @@ -90,5 +92,4 @@ describe('Test Ping Function', () => {
'scim_identities'
]);
});
});

});
34 changes: 16 additions & 18 deletions src/__tests__/helper/start-test.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,44 @@
import axios from 'axios';
import 'dotenv/config';
import { OfficialUrl, RequestType } from '../../enums';
import { GiteeStorageOptions } from '../../storage-lib/gitee/gitee-storage-options';
import {createRequest} from '../../request-lib';
export class StartTest {
constructor() {

}

static getGiteeRequest() {
static createGiteeRequest() {
return createRequest({
requestType: RequestType.axios,
requestType: 'axios',
request: axios,
accessToken: process.env.TEST_GITEE_TOKEN as string,
baseUrl: OfficialUrl.gitee,
storagePlatform: 'gitee',
owner: process.env.TEST_GITEE_OWNER as string,
repo: process.env.TEST_GITEE_REPO as string
});
}

static getGiteeOptions(): GiteeStorageOptions {
return {
request: this.getGiteeRequest(),
owner: process.env.TEST_GITEE_OWNER as string,
repo: process.env.TEST_GITEE_REPO as string,
number: process.env.TEST_GITEE_NUMBER as string,
}
static getGiteeIssueNumber() {
return process.env.TEST_GITEE_NUMBER as string;
}

static getGithubRequest() {
static createGithubRequest() {
return createRequest({
requestType: RequestType.axios,
requestType: 'axios',
request: axios,
accessToken: process.env.TEST_GITHUB_TOKEN as string,
baseUrl: OfficialUrl.github,
storagePlatform: 'github',
owner: '',
repo: ''
});
}

static getGitlabRequest() {
static createGitlabRequest() {
return createRequest({
requestType: RequestType.axios,
requestType: 'axios',
request: axios,
accessToken: process.env.TEST_GITLAB_TOKEN as string,
baseUrl: OfficialUrl.gitlab,
storagePlatform: 'gitlab',
projectId: ''
});
}
}
6 changes: 3 additions & 3 deletions src/__tests__/helper/user-storage.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { GiteeStorage } from '../../storage-lib/gitee/gitee-storage';
import { GiteeStorageOptions } from '../../storage-lib/gitee/gitee-storage-options';
import { StartTest } from './start-test';

export class UserStorage extends GiteeStorage {
constructor(options: GiteeStorageOptions) {
super(options);
constructor() {
super(StartTest.createGiteeRequest(), StartTest.getGiteeIssueNumber());
}
}
3 changes: 1 addition & 2 deletions src/__tests__/user-storage.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { StartTest } from "./helper/start-test";
import { UserStorage } from "./helper/user-storage";

test('Test Storage Lib', async () => {
const userStorage = new UserStorage(StartTest.getGiteeOptions());
const userStorage = new UserStorage();
const detail = await userStorage.find();
expect(detail.length).toBeGreaterThan(0);
}, 30000);
6 changes: 6 additions & 0 deletions src/request-lib/axios/axios-options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { AxiosInstance } from "axios";

export type AxiosOptions = {
requestType: 'axios';
request: AxiosInstance;
}
6 changes: 2 additions & 4 deletions src/request-lib/axios/axios-request.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import { AxiosInstance } from 'axios';
import { TinyRequestOptions } from '../interfaces';
import { BaseRequest } from '../base/base-request';
import { RequestOptions } from '../base/request-options';

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

async get<T>(url: string): Promise<T> {
Expand Down
55 changes: 39 additions & 16 deletions src/request-lib/base/base-request.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,56 @@
import { StoragePlatform } from "../../enums";
import { StoragePlatformUserMap, TinyRequestOptions } from "../interfaces";
import { GiteeUser, GithubUser, GitlabUser } from "../../storage-lib";
import { RequestOptions } from "./request-options";

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

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) {
async authenticate() {
switch(this.options.storagePlatform) {
case StoragePlatform.gitee:
url = `${this.baseUrl}/api/v5/user`;
break;
return this.get<GiteeUser>(`${this.baseUrl}/api/v5/user`);
case StoragePlatform.github:
url = `${this.baseUrl}/user`;
break;
return this.get<GithubUser>(`${this.baseUrl}/user`);
case StoragePlatform.gitlab:
url = `${this.baseUrl}/api/v3/user`;
break;
return this.get<GitlabUser>(`${this.baseUrl}/api/v4/user`);
default:
throw new Error('Unsupported platform');
throw new Error('Unsupported Platform');
}
}

private getBaseUrl() {
switch(this.options.storagePlatform) {
case StoragePlatform.gitee:
return 'https://gitee.com';
case StoragePlatform.github:
return 'https://api.github.com';
case StoragePlatform.gitlab:
return 'https://gitlab.com';
default:
throw new Error('Unsupported Platform');
}
}

getUrlPrefix() {
switch(this.options.storagePlatform) {
case StoragePlatform.gitee:
return `${this.baseUrl}/api/v5/repos/${this.options.owner}/${this.options.repo}`;
case StoragePlatform.github:
return '/api/v3';
case StoragePlatform.gitlab:
return '/api/v4';
default:
throw new Error('Unsupported Platform');
}

return this.get<StoragePlatformUserMap[typeof StoragePlatform[P]]>(url);
}
}
10 changes: 10 additions & 0 deletions src/request-lib/base/request-options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { GiteeOptions, GithubOptions, GitlabOptions } from "../../storage-lib";
import { AxiosOptions } from "../axios/axios-options";
import { WxOptions } from "../wx/wx-options";

export type RequestOptions = {
baseUrl?: string;
accessToken: string;
}
& (GiteeOptions | GithubOptions | GitlabOptions)
& (AxiosOptions | WxOptions);
4 changes: 2 additions & 2 deletions src/request-lib/create-request.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
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";
import { RequestOptions } from "./base/request-options";


export function createRequest(options: TinyRequestOptions) {
export function createRequest(options: RequestOptions) {
const { requestType, request } = options;
switch (requestType) {
case RequestType.axios:
Expand Down
3 changes: 2 additions & 1 deletion src/request-lib/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export {createRequest} from './create-request';
export {createRequest} from './create-request';
export {BaseRequest} from './base/base-request';
21 changes: 0 additions & 21 deletions src/request-lib/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,7 @@
import { AxiosInstance } from "axios";
import { RequestType, StoragePlatform } from "../enums";
import { WxInstance } from "./wx/wx-interface";
import { GiteeUser, GithubUser, GitlabUser } from "../storage-lib";

export type RequestInstance = WxInstance | AxiosInstance;

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

export interface TinyRequestOptions {
requestType: keyof typeof RequestType;
request: RequestInstance;
baseUrl: string;
accessToken: string;
}

export type StoragePlatformUserMap = {
[StoragePlatform.gitee]: GiteeUser;
[StoragePlatform.github]: GithubUser;
[StoragePlatform.gitlab]: GitlabUser;
}



6 changes: 6 additions & 0 deletions src/request-lib/wx/wx-options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { WxInstance } from "./wx-interface";

export type WxOptions = {
requestType: 'wx';
request: WxInstance
}
6 changes: 2 additions & 4 deletions src/request-lib/wx/wx-request.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import { BaseRequest } from '../base/base-request';
import { TinyRequestOptions } from '../interfaces';
import { RequestOptions } from '../base/request-options';
import { WxInstance } from './wx-interface';

export class WxRequest extends BaseRequest {
private wx: WxInstance;
private accessToken: string;
constructor(
protected options: TinyRequestOptions
protected options: RequestOptions
) {
super(options);
this.wx = options.request as WxInstance;
this.accessToken = options.accessToken;
}

async get<T>(url: string): Promise<T> {
Expand Down
5 changes: 5 additions & 0 deletions src/storage-lib/gitee/gitee-options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export type GiteeOptions = {
storagePlatform: 'gitee';
owner: string;
repo: string;
}
9 changes: 0 additions & 9 deletions src/storage-lib/gitee/gitee-storage-options.ts

This file was deleted.

14 changes: 5 additions & 9 deletions src/storage-lib/gitee/gitee-storage.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
import { BaseRequest } from "../../request-lib";
import { BaseStorage } from "../base/base-storage";
import { GiteeResponse } from "./gitee-response";
import { GiteeStorageOptions } from "./gitee-storage-options";

export abstract class GiteeStorage implements BaseStorage {
private baseUrl = "https://gitee.com";
constructor(private options: GiteeStorageOptions) {
this.baseUrl = options.baseUrl || this.baseUrl;
}
constructor(private request: BaseRequest, private issueNumber: string) { }

async findById(): Promise<void> {
this.options.request.get("");
this.request.get("");
}

async find() {
const {owner, repo, number} = this.options;
const url = `${this.baseUrl}/api/v5/repos/${owner}/${repo}/issues/${number}/comments`;
const response = await this.options.request.get<GiteeResponse[]>(url);
const url = `${this.request.getUrlPrefix()}/issues/${this.issueNumber}/comments`;
const response = await this.request.get<GiteeResponse[]>(url);
return response.map((item: { id: any; body: string; created_at: any; updated_at: any; }) => ({
id: item.id,
...JSON.parse(item.body),
Expand Down
5 changes: 5 additions & 0 deletions src/storage-lib/github/github-options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export type GithubOptions = {
storagePlatform: 'github';
owner: string;
repo: string;
}
4 changes: 4 additions & 0 deletions src/storage-lib/gitlab/gitlab-options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export type GitlabOptions = {
storagePlatform: 'gitlab';
projectId: string;
}
Loading

0 comments on commit 1b5dafb

Please sign in to comment.