-
-
Notifications
You must be signed in to change notification settings - Fork 17
refactor: extract client-helpers package, update svelte-query to v6 #535
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3b61066
refactor: extract client-helpers package, update svelte-query to v6
ymc9 9ac683a
update approved builds
ymc9 32c96e0
remove duplicated constant
ymc9 dddc0a3
update
ymc9 ff030fe
update to node 22
ymc9 ef3906e
update
ymc9 e946379
fix callback issue
ymc9 7fd26d5
improve sveltekit adapter
ymc9 43e7c46
update
ymc9 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,3 +9,4 @@ dist | |
| .pnpm-store | ||
| *.vsix | ||
| .DS_Store | ||
| coverage | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| import config from '@zenstackhq/eslint-config/base.js'; | ||
|
|
||
| /** @type {import("eslint").Linter.Config} */ | ||
| export default config; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| { | ||
| "name": "@zenstackhq/client-helpers", | ||
| "version": "3.0.0", | ||
| "description": "Helpers for implementing clients that consume ZenStack's CRUD service", | ||
| "type": "module", | ||
| "scripts": { | ||
| "build": "tsc --noEmit && tsup-node && pnpm test:typecheck", | ||
| "watch": "tsup-node --watch", | ||
| "lint": "eslint src --ext ts", | ||
| "test": "vitest run", | ||
| "test:typecheck": "tsc --noEmit --project tsconfig.test.json", | ||
| "pack": "pnpm pack" | ||
| }, | ||
| "author": "ZenStack Team", | ||
| "license": "MIT", | ||
| "exports": { | ||
| ".": { | ||
| "types": "./dist/index.d.ts", | ||
| "default": "./dist/index.js" | ||
| }, | ||
| "./fetch": { | ||
| "types": "./dist/fetch.d.ts", | ||
| "default": "./dist/fetch.js" | ||
| } | ||
| }, | ||
| "dependencies": { | ||
| "@zenstackhq/common-helpers": "workspace:*", | ||
| "@zenstackhq/schema": "workspace:*", | ||
| "decimal.js": "catalog:", | ||
| "superjson": "^2.2.3" | ||
| }, | ||
| "devDependencies": { | ||
| "@zenstackhq/eslint-config": "workspace:*", | ||
| "@zenstackhq/language": "workspace:*", | ||
| "@zenstackhq/orm": "workspace:*", | ||
| "@zenstackhq/sdk": "workspace:*", | ||
| "@zenstackhq/typescript-config": "workspace:*", | ||
| "@zenstackhq/vitest-config": "workspace:*" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| /** | ||
| * The default query endpoint. | ||
| */ | ||
| export const DEFAULT_QUERY_ENDPOINT = '/api/model'; | ||
ymc9 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| import { lowerCaseFirst } from '@zenstackhq/common-helpers'; | ||
| import Decimal from 'decimal.js'; | ||
| import SuperJSON from 'superjson'; | ||
| import type { QueryError } from './types'; | ||
|
|
||
| /** | ||
| * Function signature for `fetch`. | ||
| */ | ||
| export type FetchFn = (url: string, options?: RequestInit) => Promise<Response>; | ||
|
|
||
| /** | ||
| * A fetcher function that uses fetch API to make HTTP requests and automatically unmarshals | ||
| * the response using superjson. | ||
| */ | ||
| export async function fetcher<R>(url: string, options?: RequestInit, customFetch?: FetchFn): Promise<R> { | ||
| const _fetch = customFetch ?? fetch; | ||
| const res = await _fetch(url, options); | ||
| if (!res.ok) { | ||
| const errData = unmarshal(await res.text()); | ||
| if (errData.error?.rejectedByPolicy && errData.error?.rejectReason === 'cannot-read-back') { | ||
| // policy doesn't allow mutation result to be read back, just return undefined | ||
| return undefined as any; | ||
| } | ||
| const error: QueryError = new Error('An error occurred while fetching the data.'); | ||
| error.info = errData.error; | ||
| error.status = res.status; | ||
| throw error; | ||
| } | ||
|
|
||
| const textResult = await res.text(); | ||
| try { | ||
| return unmarshal(textResult).data as R; | ||
| } catch (err) { | ||
| console.error(`Unable to deserialize data:`, textResult); | ||
| throw err; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Makes a URL for the given endpoint, model, operation, and args that matches the RPC-style server API. | ||
| */ | ||
| export function makeUrl(endpoint: string, model: string, operation: string, args?: unknown) { | ||
| const baseUrl = `${endpoint}/${lowerCaseFirst(model)}/${operation}`; | ||
| if (!args) { | ||
| return baseUrl; | ||
| } | ||
|
|
||
| const { data, meta } = serialize(args); | ||
| let result = `${baseUrl}?q=${encodeURIComponent(JSON.stringify(data))}`; | ||
| if (meta) { | ||
| result += `&meta=${encodeURIComponent(JSON.stringify({ serialization: meta }))}`; | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| SuperJSON.registerCustom<Decimal, string>( | ||
| { | ||
| isApplicable: (v): v is Decimal => | ||
| v instanceof Decimal || | ||
| // interop with decimal.js | ||
| v?.toStringTag === '[object Decimal]', | ||
| serialize: (v) => v.toJSON(), | ||
| deserialize: (v) => new Decimal(v), | ||
| }, | ||
| 'Decimal', | ||
| ); | ||
|
|
||
| /** | ||
| * Serialize the given value with superjson | ||
| */ | ||
| export function serialize(value: unknown): { data: unknown; meta: unknown } { | ||
| const { json, meta } = SuperJSON.serialize(value); | ||
| return { data: json, meta }; | ||
| } | ||
|
|
||
| /** | ||
| * Deserialize the given value with superjson using the given metadata | ||
| */ | ||
| export function deserialize(value: unknown, meta: any): unknown { | ||
| return SuperJSON.deserialize({ json: value as any, meta }); | ||
| } | ||
|
|
||
| /** | ||
| * Marshal the given value to a string using superjson | ||
| */ | ||
| export function marshal(value: unknown) { | ||
| const { data, meta } = serialize(value); | ||
| if (meta) { | ||
| return JSON.stringify({ ...(data as any), meta: { serialization: meta } }); | ||
| } else { | ||
| return JSON.stringify(data); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Unmarshal the given string value using superjson, assuming the value is a JSON stringified | ||
| * object containing the serialized data and serialization metadata. | ||
| */ | ||
| export function unmarshal(value: string) { | ||
| const parsed = JSON.parse(value); | ||
| if (typeof parsed === 'object' && parsed?.data && parsed?.meta?.serialization) { | ||
| const deserializedData = deserialize(parsed.data, parsed.meta.serialization); | ||
| return { ...parsed, data: deserializedData }; | ||
| } else { | ||
| return parsed; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| export * from './constants'; | ||
| export * from './invalidation'; | ||
| export * from './logging'; | ||
| export * from './mutator'; | ||
| export * from './nested-read-visitor'; | ||
| export * from './nested-write-visitor'; | ||
| export * from './optimistic'; | ||
| export * from './query-analysis'; | ||
| export * from './types'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| import type { SchemaDef } from '@zenstackhq/schema'; | ||
| import { log, type Logger } from './logging'; | ||
| import { getMutatedModels, getReadModels } from './query-analysis'; | ||
| import type { MaybePromise, ORMWriteActionType } from './types'; | ||
|
|
||
| /** | ||
| * Type for a predicate that determines whether a query should be invalidated. | ||
| */ | ||
| export type InvalidationPredicate = ({ model, args }: { model: string; args: unknown }) => boolean; | ||
|
|
||
| /** | ||
| * Type for a function that invalidates queries matching the given predicate. | ||
| */ | ||
| export type InvalidateFunc = (predicate: InvalidationPredicate) => MaybePromise<void>; | ||
|
|
||
| /** | ||
| * Create a function that invalidates queries affected by the given mutation operation. | ||
| * | ||
| * @param model Model under mutation. | ||
| * @param operation Mutation operation (e.g, `update`). | ||
| * @param schema The schema. | ||
| * @param invalidator Function to invalidate queries matching a predicate. It should internally | ||
| * enumerate all query cache entries and invalidate those for which the predicate returns true. | ||
| * @param logging Logging option. | ||
| */ | ||
| export function createInvalidator( | ||
| model: string, | ||
| operation: string, | ||
| schema: SchemaDef, | ||
| invalidator: InvalidateFunc, | ||
| logging: Logger | undefined, | ||
| ) { | ||
| return async (...args: unknown[]) => { | ||
| const [_, variables] = args; | ||
| const predicate = await getInvalidationPredicate( | ||
| model, | ||
| operation as ORMWriteActionType, | ||
| variables, | ||
| schema, | ||
| logging, | ||
| ); | ||
| await invalidator(predicate); | ||
| }; | ||
| } | ||
|
|
||
| // gets a predicate for evaluating whether a query should be invalidated | ||
| async function getInvalidationPredicate( | ||
| model: string, | ||
| operation: ORMWriteActionType, | ||
| mutationArgs: any, | ||
| schema: SchemaDef, | ||
| logging: Logger | undefined, | ||
| ): Promise<InvalidationPredicate> { | ||
| const mutatedModels = await getMutatedModels(model, operation, mutationArgs, schema); | ||
|
|
||
| return ({ model, args }) => { | ||
| if (mutatedModels.includes(model)) { | ||
| // direct match | ||
| if (logging) { | ||
| log( | ||
| logging, | ||
| `Marking "${model}" query for invalidation due to mutation "${operation}", query args: ${JSON.stringify(args)}`, | ||
| ); | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| if (args) { | ||
| // traverse query args to find nested reads that match the model under mutation | ||
| if (findNestedRead(model, mutatedModels, schema, args)) { | ||
| if (logging) { | ||
| log( | ||
| logging, | ||
| `Marking "${model}" query for invalidation due to mutation "${operation}", query args: ${JSON.stringify(args)}`, | ||
| ); | ||
| } | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| }; | ||
| } | ||
|
|
||
| // find nested reads that match the given models | ||
| function findNestedRead(visitingModel: string, targetModels: string[], schema: SchemaDef, args: any) { | ||
| const modelsRead = getReadModels(visitingModel, schema, args); | ||
| return targetModels.some((m) => modelsRead.includes(m)); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| /** | ||
| * Logger configuration. `true` enables console logging. A function can be provided for custom logging. | ||
| */ | ||
| export type Logger = boolean | ((message: string) => void); | ||
|
|
||
| /** | ||
| * Logs a message using the provided logger. | ||
| */ | ||
| export function log(logger: Logger, message: string) { | ||
| if (typeof logger === 'function') { | ||
| logger(message); | ||
| } else if (logger) { | ||
| console.log(message); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.