Skip to content

Commit

Permalink
Merge pull request #41 from GuoXiCheng/main
Browse files Browse the repository at this point in the history
publish 1.0.16
  • Loading branch information
GuoXiCheng authored Dec 15, 2023
2 parents 1aa1a82 + bfbad1b commit 21f4ebb
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 28 deletions.
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.15",
"version": "1.0.16",
"description": "",
"main": "dist/bundle.cjs.js",
"module": "dist/bundle.esm.js",
Expand Down
11 changes: 11 additions & 0 deletions src/__tests__/user-storage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,15 @@ describe('Test User Storage', () => {
const findByIdResult = await userStorage.findById(findResult[0].id);
expect(findByIdResult).toEqual(findResult[0]);
});

test('Test updateById User', async () => {
const findResult = await userStorage.find();
const updateResult = await userStorage.updateById(findResult[0].id, {
name: 'test-user-update',
age: 20
});
const findByIdResult = await userStorage.findById(findResult[0].id);
expect(updateResult.name).toEqual(findByIdResult.name);
expect(updateResult.age).toEqual(findByIdResult.age);
});
});
6 changes: 0 additions & 6 deletions src/enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,3 @@ 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: 16 additions & 2 deletions src/request-lib/axios/axios-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ export class AxiosRequest extends BaseRequest {
});
}

async delete<T>(url: string): Promise<void> {
async delete(url: string): Promise<void> {
return new Promise((resolve, reject) => {
this.axios.delete<T>(url, {
this.axios.delete(url, {
headers: {
'Authorization': `Bearer ${this.accessToken}`
}
Expand All @@ -52,4 +52,18 @@ export class AxiosRequest extends BaseRequest {
});
});
}

patch<T>(url: string, data: any): Promise<T> {
return new Promise((resolve, reject) => {
this.axios.patch<T>(url, data, {
headers: {
'Authorization': `Bearer ${this.accessToken}`
}
}).then((res) => {
resolve(res.data);
}).catch(error => {
reject(error);
});
});
}
}
7 changes: 4 additions & 3 deletions src/request-lib/base/base-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ export abstract class BaseRequest {

abstract get<T>(url: string): Promise<T>;
abstract post<T>(url: string, data: any): Promise<T>;
abstract delete<T>(url: string): Promise<void>;
abstract delete(url: string): Promise<void>;
abstract patch<T>(url: string, data: any): Promise<T>;

async authenticate() {
switch(this.options.storagePlatform) {
Expand Down Expand Up @@ -47,9 +48,9 @@ export abstract class BaseRequest {
case StoragePlatform.gitee:
return `${this.baseUrl}/api/v5/repos/${this.options.owner}/${this.options.repo}`;
case StoragePlatform.github:
return '/api/v3';
return `${this.baseUrl}/user`;
case StoragePlatform.gitlab:
return '/api/v4';
return `${this.baseUrl}/api/v4/user`;
default:
throw new Error('Unsupported Platform');
}
Expand Down
3 changes: 2 additions & 1 deletion src/request-lib/wx/wx-interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ export interface WxInstance {

export interface WxRequestOptions {
url: string;
method: 'GET' | 'POST';
method: 'GET' | 'POST' | 'DELETE' | 'PUT' | 'OPTIONS' | 'HEAD' | 'TRACE' | 'CONNECT';
data?: string | object | ArrayBuffer;
header?: object;
success: (res: { data: string | Object | ArrayBuffer, statusCode: number }) => void;
fail: (errMsg: string, errNo: number) => void;
Expand Down
75 changes: 60 additions & 15 deletions src/request-lib/wx/wx-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,74 @@ export class WxRequest extends BaseRequest {
header: {
'Authorization': `Bearer ${this.accessToken}`
},
success: (res: { data: string | Object | ArrayBuffer, statusCode: number }) => {
resolve(res as T);
success: (res: any) => {
resolve(res.data);
},
fail: (errMsg: string, errNo: number) => {
fail: (errMsg: string) => {
console.error(errMsg);
reject(errMsg);
}
});
});
}

// post(url: string) {
// this.wx.request({
// url,
// method: 'POST',
// header: {
// 'Authorization': `Bearer ${this.accessToken}`
// }
// });
// }
post<T>(url: string, data: any): Promise<T> {
throw new Error('Method not implemented.');
return new Promise((resolve, reject) => {
this.wx.request({
url,
method: 'POST',
data: data,
header: {
'Authorization': `Bearer ${this.accessToken}`
},
success: (res: any) => {
resolve(res.data);
},
fail: (errMsg: string) => {
console.error(errMsg);
reject(errMsg);
}
});
});
}
delete<T>(url: string): Promise<void> {
throw new Error('Method not implemented.');

delete(url: string): Promise<void> {
return new Promise((resolve, reject) => {
this.wx.request({
url,
method: 'DELETE',
header: {
'Authorization': `Bearer ${this.accessToken}`
},
success: (res: any) => {
resolve();
},
fail: (errMsg: string) => {
console.error(errMsg);
reject(errMsg);
}
});
});
}

patch<T>(url: string, data: any): Promise<T> {
return new Promise((resolve, reject) => {
this.wx.request({
url,
method: 'POST',
data: data,
header: {
'Authorization': `Bearer ${this.accessToken}`,
'X-HTTP-Method-Override': 'PATCH'
},
success: (res: any) => {
resolve(res.data);
},
fail: (errMsg: string) => {
console.error(errMsg);
reject(errMsg);
}
});
});
}
}
12 changes: 12 additions & 0 deletions src/storage-lib/gitee/gitee-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,16 @@ export class GiteeStorage<T extends BaseModel> extends BaseStorage<T> {
return await this.request.delete(url);
}

/**
* Updates a record by its ID.
* @param id - The ID of the record to update.
* @param data - The updated data for the record, excluding the id, created_at, and updated_at fields.
* @returns The updated record.
*/
async updateById(id: number, data: Omit<T, 'id' | 'created_at' | 'updated_at'>) {
const url = `${this.endpoint}/issues/comments/${id}`;
const response = await this.request.patch<BaseComment>(url, {body: this.serialize<Omit<T, 'id' | 'created_at' | 'updated_at'>>(data)});
return this.deserialize<T>(response);
}

}

0 comments on commit 21f4ebb

Please sign in to comment.