Skip to content

Commit

Permalink
feat: add type infer-er utility
Browse files Browse the repository at this point in the history
  • Loading branch information
hUwUtao committed Dec 4, 2024
1 parent a26c6a7 commit 2b70982
Show file tree
Hide file tree
Showing 3 changed files with 29 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": "2.2.4",
"version": "2.2.5",
"description": "powered by `wagtail`. awesome!",
"type": "module",
"main": "dist/index.js",
Expand Down
2 changes: 2 additions & 0 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,6 @@ console.log(
data.blogList?.items?.flatMap((t) => t?.involved?.map((i) => i?.name))
)

console.log(data.blogList?.items?.map((k) => k?.title))

console.log('tree view:', data, null, 5)
44 changes: 26 additions & 18 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,24 @@ import {

// import persisted from './graphql/persisted-documents.json'

// __typename is not a real field
type NodeType<T> = { __typename: T }

/**
* Assume on GraphQL Codegen abstraction.
* For query that has a lot of overload, this helps.
* The reason this is not wrapped in queries already, is because the root one is okay, but not the underlying.
* @param t `__typename` assumption
* @param v hinted object (must be hinted)
*/
export function assume<
Hinted,
Assumed extends Hinted & { __typename: string },
Infered extends Assumed['__typename']
>(t: Infered, v: Hinted) {
return v as Hinted & NodeType<typeof t>
}

export interface GraphQLClientConfig {
endpoint: string
headers?: Record<string, string>
Expand Down Expand Up @@ -75,31 +93,21 @@ export default class WTClient {
* List all blog page
*/
async listBlogs(page = 0, perPage = 1024, order = '-id') {
return await execute(this.config, BlogPaginatedListViewDocument, {
page: page,
perPage: perPage,
order: order
})
return (
await execute(this.config, BlogPaginatedListViewDocument, {
page: page,
perPage: perPage,
order: order
})
).blogs
}

/**
* Get blog view from preview token
* @param token urlparsed token query (added by wagtail addon)
*/
async getBlogPreview(token: string) {
return (
await execute(
{
...this.config,
headers: {
...this.config.headers,
Authorization: `Bearer ${token}`
}
},
BlogPreviewViewDocument,
{ token }
)
).blog
return (await execute(this.config, BlogPreviewViewDocument, { token })).blog
}

/**
Expand Down

0 comments on commit 2b70982

Please sign in to comment.