diff --git a/package.json b/package.json index 9292d94..898c8f7 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/__tests__/user-storage.test.ts b/src/__tests__/user-storage.test.ts index e7a1c25..d28a4e3 100644 --- a/src/__tests__/user-storage.test.ts +++ b/src/__tests__/user-storage.test.ts @@ -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); + }); }); \ No newline at end of file diff --git a/src/enums.ts b/src/enums.ts index 006f348..214c25d 100644 --- a/src/enums.ts +++ b/src/enums.ts @@ -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" -} \ No newline at end of file diff --git a/src/request-lib/axios/axios-request.ts b/src/request-lib/axios/axios-request.ts index 8064c3f..b5621b9 100644 --- a/src/request-lib/axios/axios-request.ts +++ b/src/request-lib/axios/axios-request.ts @@ -39,9 +39,9 @@ export class AxiosRequest extends BaseRequest { }); } - async delete(url: string): Promise { + async delete(url: string): Promise { return new Promise((resolve, reject) => { - this.axios.delete(url, { + this.axios.delete(url, { headers: { 'Authorization': `Bearer ${this.accessToken}` } @@ -52,4 +52,18 @@ export class AxiosRequest extends BaseRequest { }); }); } + + patch(url: string, data: any): Promise { + return new Promise((resolve, reject) => { + this.axios.patch(url, data, { + headers: { + 'Authorization': `Bearer ${this.accessToken}` + } + }).then((res) => { + resolve(res.data); + }).catch(error => { + reject(error); + }); + }); + } } diff --git a/src/request-lib/base/base-request.ts b/src/request-lib/base/base-request.ts index 2670ba9..dc7ce61 100644 --- a/src/request-lib/base/base-request.ts +++ b/src/request-lib/base/base-request.ts @@ -14,7 +14,8 @@ export abstract class BaseRequest { abstract get(url: string): Promise; abstract post(url: string, data: any): Promise; - abstract delete(url: string): Promise; + abstract delete(url: string): Promise; + abstract patch(url: string, data: any): Promise; async authenticate() { switch(this.options.storagePlatform) { @@ -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'); } diff --git a/src/request-lib/wx/wx-interface.ts b/src/request-lib/wx/wx-interface.ts index 8c6dff1..f855be6 100644 --- a/src/request-lib/wx/wx-interface.ts +++ b/src/request-lib/wx/wx-interface.ts @@ -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; diff --git a/src/request-lib/wx/wx-request.ts b/src/request-lib/wx/wx-request.ts index 87672fb..f99dabf 100644 --- a/src/request-lib/wx/wx-request.ts +++ b/src/request-lib/wx/wx-request.ts @@ -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(url: string, data: any): Promise { - 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(url: string): Promise { - throw new Error('Method not implemented.'); + + delete(url: string): Promise { + 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(url: string, data: any): Promise { + 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); + } + }); + }); } } diff --git a/src/storage-lib/gitee/gitee-storage.ts b/src/storage-lib/gitee/gitee-storage.ts index dabd9ad..f6e27ca 100644 --- a/src/storage-lib/gitee/gitee-storage.ts +++ b/src/storage-lib/gitee/gitee-storage.ts @@ -54,4 +54,16 @@ export class GiteeStorage extends BaseStorage { 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) { + const url = `${this.endpoint}/issues/comments/${id}`; + const response = await this.request.patch(url, {body: this.serialize>(data)}); + return this.deserialize(response); + } + } \ No newline at end of file