-
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 #35 from GuoXiCheng/dev-c
Dev c
- Loading branch information
Showing
19 changed files
with
129 additions
and
103 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,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: '' | ||
}); | ||
} | ||
} |
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,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()); | ||
} | ||
} |
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,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); |
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,6 @@ | ||
import { AxiosInstance } from "axios"; | ||
|
||
export type AxiosOptions = { | ||
requestType: 'axios'; | ||
request: AxiosInstance; | ||
} |
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,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); | ||
} | ||
} |
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,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); |
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 +1,2 @@ | ||
export {createRequest} from './create-request'; | ||
export {createRequest} from './create-request'; | ||
export {BaseRequest} from './base/base-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
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; | ||
} | ||
|
||
|
||
|
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,6 @@ | ||
import { WxInstance } from "./wx-interface"; | ||
|
||
export type WxOptions = { | ||
requestType: 'wx'; | ||
request: WxInstance | ||
} |
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 type GiteeOptions = { | ||
storagePlatform: 'gitee'; | ||
owner: string; | ||
repo: string; | ||
} |
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
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 type GithubOptions = { | ||
storagePlatform: 'github'; | ||
owner: string; | ||
repo: 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 type GitlabOptions = { | ||
storagePlatform: 'gitlab'; | ||
projectId: string; | ||
} |
Oops, something went wrong.