Skip to content

Commit

Permalink
fix: string id should be number, fix #1 (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
XxLittleCxX authored Nov 10, 2023
1 parent bef5718 commit ed497d7
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -575,25 +575,33 @@ export class Internal {
return [prop, isAsync] as const
}

private static prepareArg(params: string[], args: any[]) {
const fixedArg = Object.fromEntries(params.map((name, index) => [name, args[index]]))
for (const key in fixedArg) {
if (key.endsWith("_id")) fixedArg[key] = +fixedArg[key]
}
return fixedArg
}

static define(name: string, ...params: string[]) {
const [prop, isAsync] = Internal.prepareMethod(name)
Internal.prototype[prop] = async function (this: Internal, ...args: any[]) {
const data = await this._get(name, Object.fromEntries(params.map((name, index) => [name, args[index]])))
const data = await this._get(name, Internal.prepareArg(params, args))
if (!isAsync) return data
}
isAsync && (Internal.prototype[prop + 'Async'] = async function (this: Internal, ...args: any[]) {
await this._get(name + '_async', Object.fromEntries(params.map((name, index) => [name, args[index]])))
await this._get(name + '_async', Internal.prepareArg(params, args))
})
}

static defineExtract(name: string, key: string, ...params: string[]) {
const [prop, isAsync] = Internal.prepareMethod(name)
Internal.prototype[prop] = async function (this: Internal, ...args: any[]) {
const data = await this._get(name, Object.fromEntries(params.map((name, index) => [name, args[index]])))
const data = await this._get(name, Internal.prepareArg(params, args))
return data[key]
}
isAsync && (Internal.prototype[prop + 'Async'] = async function (this: Internal, ...args: any[]) {
await this._get(name + '_async', Object.fromEntries(params.map((name, index) => [name, args[index]])))
await this._get(name + '_async', Internal.prepareArg(params, args))
})
}
}
Expand Down

0 comments on commit ed497d7

Please sign in to comment.