Skip to content

Commit

Permalink
squash+fix: hook + call + types fixes
Browse files Browse the repository at this point in the history
fix: tehe, idiotic hook system

feat+refactor: id in generic

fix: improve type accuracy of response

feat: expand typing and calls, err handler

[ci]: bump, mysteriously unsynced file???

fix: make fields optional

bump
  • Loading branch information
hUwUtao committed Jun 7, 2024
1 parent 89744e4 commit 2604027
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 19 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@team-fuho/wt",
"version": "1.2.1",
"version": "1.3.3",
"description": "",
"type": "commonjs",
"main": "dist/index.js",
Expand Down
105 changes: 87 additions & 18 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@ export interface Context {

export type Hook<T> = { transform: SerdeTrait<any, string>; fields: T[] }[]

type GenericResponse = {
error?: string
}
type GenericItemResponse<ExpandedSchema> = TResponse & ExpandedSchema
type GenericListResponse<ExpandedSchema> = {
meta: {
total_count: number
}
items: GenericItemResponse<ExpandedSchema>[]
}

export class Model<T> {
readonly ctx: Context
readonly kind: string
Expand Down Expand Up @@ -56,30 +67,88 @@ export class Model<T> {
return request
}

private async fetch<T>(
options: GenericParameter,
id?: number,
init?: RequestInit
) {
return await new Promise<GenericResponse & T>((ok, err) =>
fetch(
this.requestBuilder(options, id),
Object.assign(
{
headers: {
'User-Agent': 'BoilerplateClient',
},
} as RequestInit,
init
)
)
.then((ok) => ok.json())
.then((j: GenericResponse) => {
if (j.error) {
err(new Error('WagtailQueryError: ' + j.error))
return
}
// @ts-ignore
ok(j)
})
)
}

/**
* @description to return stuff, ofc
* @description to return many stuff, ofc
*/
async query(options: GenericParameter, id?: number, init?: RequestInit) {
return fetch(this.requestBuilder(options, id), Object.assign({}, init))
.then(
(ok) =>
ok.json() as Promise<{
meta: {
total_count: Number
}
items: (TResponse & T)[]
}>
)
.then((ok) => {
this.hook.map((hook) => {
hook.fields.map(
(field) =>
async query(
options: GenericParameter,
init?: RequestInit
): Promise<GenericResponse & GenericListResponse<T>> {
const query = this.fetch<GenericListResponse<T>>(
options,
undefined,
init
)
if (this.hook.length == 0) return query
return query.then((r) => {
this.hook.forEach((h) => {
r.items = r.items.map((i) => {
h.fields.forEach(
(f) =>
// @ts-ignore
(ok[field] = hook.transform.from(ok[field]))
(i[f] = h.transform.from(i[f]))
)
return ok
return i
})
})
return r
})
}

/**
* @description to return one stuff
*/
async queryOne(
options: GenericParameter & {
/**
* @deprecated Everything by default
*/
fields?: string
},
id: number,
init?: RequestInit
): Promise<GenericResponse & GenericItemResponse<T>> {
const query = this.fetch<GenericItemResponse<T>>(options, id, init)
if (this.hook.length == 0) return query
return query.then((r) => {
this.hook.forEach((h) => {
h.fields.forEach(
(f) =>
// @ts-ignore
(r[f] = h.transform.from(r[f]))
)
})
return r
})
}
}

Expand Down

0 comments on commit 2604027

Please sign in to comment.