Skip to content

Commit

Permalink
Merge pull request #39 from GuoXiCheng/main
Browse files Browse the repository at this point in the history
publish 1.0.15
  • Loading branch information
GuoXiCheng authored Dec 14, 2023
2 parents f9c9fc4 + 8ff0522 commit 1aa1a82
Show file tree
Hide file tree
Showing 38 changed files with 679 additions and 147 deletions.
9 changes: 7 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,13 @@ jobs:
- name: Run Jest tests
run: npm run test
env:
GITEE_TOKEN: ${{ secrets.GITEE_TOKEN }}
GITEE_GET_ALL_URL: ${{ secrets.GITEE_GET_ALL_URL }}
TEST_GITEE_TOKEN: ${{ secrets.TEST_GITEE_TOKEN }}
TEST_GITEE_OWNER: ${{ secrets.TEST_GITEE_OWNER }}
TEST_GITEE_REPO: ${{ secrets.TEST_GITEE_REPO }}
TEST_GITEE_NUMBER: ${{ secrets.TEST_GITEE_NUMBER }}
TEST_GITHUB_TOKEN: ${{ secrets.TEST_GITHUB_TOKEN }}
TEST_GITLAB_TOKEN: ${{ secrets.TEST_GITLAB_TOKEN }}


- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v3
Expand Down
2 changes: 2 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
module.exports = {
testTimeout: 30000,
preset: 'ts-jest',
testEnvironment: 'node',
collectCoverageFrom: [
"src/**/*.ts", // 包括 src 目录下所有的 TypeScript 文件
"!src/**/*.d.ts", // 排除 TypeScript 声明文件
"!src/__tests__/**/*.ts"
],
testMatch: ["<rootDir>/src/__tests__/**/*.test.ts"]
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tiny-crud",
"version": "1.0.14",
"version": "1.0.15",
"description": "",
"main": "dist/bundle.cjs.js",
"module": "dist/bundle.esm.js",
Expand Down
95 changes: 95 additions & 0 deletions src/__tests__/authenticate.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import { StartTest } from "./helper/start-test";

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',
'url', 'html_url',
'remark', 'followers_url',
'following_url', 'gists_url',
'starred_url', 'subscriptions_url',
'organizations_url', 'repos_url',
'events_url', 'received_events_url',
'type', 'blog',
'weibo', 'bio',
'public_repos', 'public_gists',
'followers', 'following',
'stared', 'watched',
'created_at', 'updated_at',
'email'
])
});

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',
'gravatar_id', 'url',
'html_url', 'followers_url',
'following_url', 'gists_url',
'starred_url', 'subscriptions_url',
'organizations_url', 'repos_url',
'events_url', 'received_events_url',
'type', 'site_admin',
'name', 'company',
'blog', 'location',
'email', 'hireable',
'bio', 'twitter_username',
'public_repos', 'public_gists',
'followers', 'following',
'created_at', 'updated_at'
]);
});

test('Test Authenticate Gitlab', async () => {
const request = StartTest.createGitlabRequest();
const res = await request.authenticate();
expect(Object.keys(res)).toEqual([
'id',
'username',
'name',
'state',
'locked',
'avatar_url',
'web_url',
'created_at',
'bio',
'location',
'public_email',
'skype',
'linkedin',
'twitter',
'discord',
'website_url',
'organization',
'job_title',
'pronouns',
'bot',
'work_information',
'local_time',
'last_sign_in_at',
'confirmed_at',
'last_activity_on',
'email',
'theme_id',
'color_scheme_id',
'projects_limit',
'current_sign_in_at',
'identities',
'can_create_group',
'can_create_project',
'two_factor_enabled',
'external',
'private_profile',
'commit_email',
'shared_runners_minutes_limit',
'extra_shared_runners_minutes_limit',
'scim_identities'
]);
});
});
45 changes: 45 additions & 0 deletions src/__tests__/helper/start-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import axios from 'axios';
import 'dotenv/config';
import { createRequest } from '../..';

export class StartTest {
constructor() {

}

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

static getGiteeIssueNumber() {
return process.env.TEST_GITEE_NUMBER as string;
}

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

static createGitlabRequest() {
return createRequest({
requestType: 'axios',
request: axios,
accessToken: process.env.TEST_GITLAB_TOKEN as string,
storagePlatform: 'gitlab',
projectId: ''
});
}
}
5 changes: 5 additions & 0 deletions src/__tests__/helper/user-model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { BaseModel } from "../..";
export interface UserModel extends BaseModel {
name: string;
age: number;
}
9 changes: 9 additions & 0 deletions src/__tests__/helper/user-storage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { GiteeStorage } from '../..';
import { StartTest } from './start-test';
import { UserModel } from './user-model';

export class UserStorage extends GiteeStorage<UserModel> {
constructor() {
super(StartTest.createGiteeRequest(), StartTest.getGiteeIssueNumber());
}
}
3 changes: 0 additions & 3 deletions src/__tests__/index.test.ts

This file was deleted.

11 changes: 0 additions & 11 deletions src/__tests__/request-lib.test.ts

This file was deleted.

23 changes: 23 additions & 0 deletions src/__tests__/user-storage.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { UserStorage } from "./helper/user-storage";

describe('Test User Storage', () => {
const userStorage = new UserStorage();

test('Test deleteAll User', async () => {
await userStorage.deleteAll();
const detail = await userStorage.find();
expect(detail.length).toEqual(0);
});

test('Test create & User & findById User', async () => {
await userStorage.create({
name: 'test-user',
age: 18
});
const findResult = await userStorage.find();
expect(findResult.length).toEqual(1);

const findByIdResult = await userStorage.findById(findResult[0].id);
expect(findByIdResult).toEqual(findResult[0]);
});
});
16 changes: 16 additions & 0 deletions src/enums.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export enum StoragePlatform {
gitee = "gitee",
github = "github",
gitlab = "gitlab"
}

export enum RequestType {
axios = "axios",
wx = "wx"
}

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

export enum RequestLib {
axios = "axios",
wx = "wx"
}

export type StoragePlatformType = keyof typeof StoragePlatform;
export type RequestLibType = keyof typeof RequestLib;
export {TinyRequestInstance};
import {createRequest} from "./request-lib/create-request";
import { BaseModel, GiteeStorage } from "./storage-lib";
export {createRequest, BaseModel, GiteeStorage};

25 changes: 0 additions & 25 deletions src/request-lib/axios-request.ts

This file was deleted.

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;
}
55 changes: 55 additions & 0 deletions src/request-lib/axios/axios-request.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { AxiosInstance } from 'axios';
import { BaseRequest } from '../base/base-request';
import { RequestOptions } from '../base/request-options';

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

async get<T>(url: string): Promise<T> {
return new Promise((resolve, reject) => {
this.axios.get<T>(url, {
headers: {
'Authorization': `Bearer ${this.accessToken}`
}
}).then((res) => {
resolve(res.data);
}).catch(error => {
reject(error);
});
});
}

async post<T>(url: string, data: any): Promise<T> {
return new Promise((resolve, reject) => {
this.axios.post<T>(url, data, {
headers: {
'Authorization': `Bearer ${this.accessToken}`
}
}).then((res) => {
resolve(res.data);
}).catch(error => {
reject(error);
});
});
}

async delete<T>(url: string): Promise<void> {
return new Promise((resolve, reject) => {
this.axios.delete<T>(url, {
headers: {
'Authorization': `Bearer ${this.accessToken}`
}
}).then(() => {
resolve();
}).catch(error => {
reject(error);
});
});
}
}
Loading

0 comments on commit 1aa1a82

Please sign in to comment.