diff --git a/.changeset/itchy-guests-argue.md b/.changeset/itchy-guests-argue.md new file mode 100644 index 000000000000..cdc49fc9dad7 --- /dev/null +++ b/.changeset/itchy-guests-argue.md @@ -0,0 +1,8 @@ +--- +'@data-client/react': patch +--- + +Fix pnp compatibility + +Change import from @data-client/normalizr -> @data-client/core +as `core` is explicit dependency \ No newline at end of file diff --git a/__tests__/UnionSchema.ts b/__tests__/UnionSchema.ts index 86711f316cdb..1e6404ecf625 100644 --- a/__tests__/UnionSchema.ts +++ b/__tests__/UnionSchema.ts @@ -1,4 +1,4 @@ -import { Entity, schema } from '@data-client/endpoint'; +import { Entity, schema } from '@data-client/rest'; export enum SequenceType { TemporalCatDiff = 'temporal_cat_diff', diff --git a/examples/github-app/package-lock.json b/examples/github-app/package-lock.json index 9e7ff2aafed7..03c43383da36 100644 --- a/examples/github-app/package-lock.json +++ b/examples/github-app/package-lock.json @@ -12,10 +12,12 @@ "@anansi/router": "0.10.10", "@ant-design/icons": "^5.0.0", "@data-client/graphql": "^0.14.0", + "@data-client/img": "^0.14.15", "@data-client/react": "^0.14.0", "@data-client/rest": "^0.14.0", "@js-temporal/polyfill": "^0.4.4", "antd": "5.22.7", + "history": "^5.3.0", "parse-link-header": "^2.0.0", "react": "19.0.0", "react-dom": "19.0.0", @@ -3447,6 +3449,28 @@ "url": "https://github.com/sponsors/ntucker" } }, + "node_modules/@data-client/img": { + "version": "0.14.15", + "resolved": "https://registry.npmjs.org/@data-client/img/-/img-0.14.15.tgz", + "integrity": "sha512-d8F+Ld416+nks4hRqs+SDhJtpx0UStz+QH8Tnh2BowsfRrU9o5eBDrXn45ZC3Atj2s1cENtXs4EyrdWzbxoVGA==", + "dependencies": { + "@babel/runtime": "^7.17.0", + "@data-client/endpoint": "^0.14.12" + }, + "funding": { + "url": "https://github.com/sponsors/ntucker" + }, + "peerDependencies": { + "@data-client/react": "^0.1.0 || ^0.2.0 || ^0.3.0 || ^0.4.0 || ^0.5.0 || ^0.7.0 || ^0.8.0 || ^0.9.0 || ^0.10.0 || ^0.11.0 || ^0.12.0 || ^0.13.0 || ^0.14.0", + "@types/react": "^16.14.0 || ^17.0.0 || ^18.0.0-0 || ^19.0.0", + "react": "^16.14.0 || ^17.0.0 || ^18.0.0-0 || ^19.0.0" + }, + "peerDependenciesMeta": { + "@types/react": { + "optional": true + } + } + }, "node_modules/@data-client/normalizr": { "version": "0.14.16", "resolved": "https://registry.npmjs.org/@data-client/normalizr/-/normalizr-0.14.16.tgz", diff --git a/examples/github-app/package.json b/examples/github-app/package.json index dc96155c953b..4d1c257f69f3 100644 --- a/examples/github-app/package.json +++ b/examples/github-app/package.json @@ -51,10 +51,12 @@ "@anansi/router": "0.10.10", "@ant-design/icons": "^5.0.0", "@data-client/graphql": "^0.14.0", + "@data-client/img": "^0.14.15", "@data-client/react": "^0.14.0", "@data-client/rest": "^0.14.0", "@js-temporal/polyfill": "^0.4.4", "antd": "5.22.7", + "history": "^5.3.0", "parse-link-header": "^2.0.0", "react": "19.0.0", "react-dom": "19.0.0", diff --git a/package.json b/package.json index 4f43b6678edc..ecc992c68150 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,7 @@ "ci:build:esmodule": "yarn workspaces foreach -WptivR --from @data-client/react --from @data-client/rest --from @data-client/graphql run build:lib && yarn workspace @data-client/normalizr run build:js:node && yarn workspace @data-client/endpoint run build:js:node", "build:copy:ambient": "mkdirp ./packages/endpoint/lib && copyfiles --flat ./packages/endpoint/src/schema.d.ts ./packages/endpoint/lib/ && copyfiles --flat ./packages/endpoint/src/endpoint.d.ts ./packages/endpoint/lib/ && mkdirp ./packages/rest/lib && copyfiles --flat ./packages/rest/src/RestEndpoint.d.ts ./packages/rest/lib && copyfiles --flat ./packages/rest/src/next/RestEndpoint.d.ts ./packages/rest/lib/next && mkdirp ./packages/react/lib && copyfiles --flat ./packages/react/src/server/redux/redux.d.ts ./packages/react/lib/server/redux", "copy:websitetypes": "./scripts/copywebsitetypes.sh", - "test": "NODE_ENV=test NODE_OPTIONS=--experimental-vm-modules jest", + "test": "NODE_ENV=test NODE_OPTIONS=--experimental-vm-modules run jest", "test:ci": "ANANSI_JEST_TYPECHECK=false yarn test --ci", "test:coverage": "ANANSI_JEST_TYPECHECK=false yarn test --coverage", "prepare": "yarn build:copy:ambient && tsc --build", @@ -58,8 +58,9 @@ "@babel/preset-flow": "^7.18.6", "@changesets/changelog-github": "^0.5.0", "@changesets/cli": "^2.26.1", - "@data-client/react": "workspace:^", - "@data-client/test": "workspace:^", + "@data-client/react": "workspace:*", + "@data-client/rest": "workspace:*", + "@data-client/test": "workspace:*", "@js-temporal/polyfill": "^0.4.4", "@react-navigation/native": "^7.0.0", "@react-navigation/native-stack": "^7.0.0", diff --git a/packages/img/src/__tests__/Img.web.tsx b/packages/img/src/__tests__/Img.web.tsx index 1d9077535a51..ab58399c0871 100644 --- a/packages/img/src/__tests__/Img.web.tsx +++ b/packages/img/src/__tests__/Img.web.tsx @@ -1,6 +1,6 @@ import { DataProvider } from '@data-client/react'; -import { getByTestId, render, waitFor } from '@testing-library/react'; -import React, { Suspense } from 'react'; +import { render, waitFor } from '@testing-library/react'; +import { Suspense } from 'react'; import { Img } from '..'; diff --git a/packages/react/package.json b/packages/react/package.json index eb4941232bd6..055077b0106b 100644 --- a/packages/react/package.json +++ b/packages/react/package.json @@ -157,7 +157,7 @@ "build:js:node": "BROWSERSLIST_ENV=node12 yarn g:rollup", "build:js:browser": "BROWSERSLIST_ENV=legacy yarn g:rollup", "build:bundle": "yarn g:runs build:js:\\* && echo '{\"type\":\"commonjs\"}' > dist/package.json", - "build:clean": "yarn g:clean ./index.d.ts", + "build:clean": "yarn g:clean index.d.ts next.d.ts native", "build:legacy-types": "yarn g:downtypes lib ts3.4", "build": "run build:lib && run build:legacy:lib && run build:native:lib && run build:bundle", "dev": "run build:lib -w", diff --git a/packages/react/src/__tests__/integration-endpoint.web.tsx b/packages/react/src/__tests__/integration-endpoint.web.tsx index 180f3cb1e850..5858e65f8794 100644 --- a/packages/react/src/__tests__/integration-endpoint.web.tsx +++ b/packages/react/src/__tests__/integration-endpoint.web.tsx @@ -1,7 +1,6 @@ -import { schema, Entity } from '@data-client/endpoint'; -import { Endpoint } from '@data-client/endpoint'; import { DataProvider } from '@data-client/react'; import { DataProvider as ExternalDataProvider } from '@data-client/react/redux'; +import { schema, Entity, Endpoint } from '@data-client/rest'; import { CoolerArticleResource, EditorArticleResource, diff --git a/packages/react/src/__tests__/integration-optimistic-endpoint.web.tsx b/packages/react/src/__tests__/integration-optimistic-endpoint.web.tsx index 497410a340d2..9f7ac6d0ba8a 100644 --- a/packages/react/src/__tests__/integration-optimistic-endpoint.web.tsx +++ b/packages/react/src/__tests__/integration-optimistic-endpoint.web.tsx @@ -1,6 +1,6 @@ -import { Endpoint, Entity } from '@data-client/endpoint'; import { CacheProvider } from '@data-client/react'; import { DataProvider as ExternalDataProvider } from '@data-client/react/redux'; +import { Endpoint, Entity } from '@data-client/rest'; import { afterEach, jest } from '@jest/globals'; import { OptimisticArticleResource, diff --git a/packages/react/src/components/__tests__/AsyncBoundary.web.tsx b/packages/react/src/components/__tests__/AsyncBoundary.web.tsx index 85d780f0b6c4..1bb6228988c8 100644 --- a/packages/react/src/components/__tests__/AsyncBoundary.web.tsx +++ b/packages/react/src/components/__tests__/AsyncBoundary.web.tsx @@ -1,5 +1,4 @@ -import { Endpoint } from '@data-client/endpoint'; -import { NetworkError } from '@data-client/rest'; +import { NetworkError, Endpoint } from '@data-client/rest'; import { render } from '@testing-library/react'; import { ReactElement, StrictMode } from 'react'; diff --git a/packages/react/src/hooks/useCancelling.ts b/packages/react/src/hooks/useCancelling.ts index 91e141aa25a9..6b6d0b388f4c 100644 --- a/packages/react/src/hooks/useCancelling.ts +++ b/packages/react/src/hooks/useCancelling.ts @@ -1,4 +1,4 @@ -import type { EndpointInterface } from '@data-client/normalizr'; +import type { EndpointInterface } from '@data-client/core'; import { useMemo, useRef } from 'react'; /** diff --git a/website/src/components/Playground/editor-types/@data-client/react.d.ts b/website/src/components/Playground/editor-types/@data-client/react.d.ts index 654807eacec6..e4d2a0449c33 100644 --- a/website/src/components/Playground/editor-types/@data-client/react.d.ts +++ b/website/src/components/Playground/editor-types/@data-client/react.d.ts @@ -1,5 +1,5 @@ import * as _data_client_core from '@data-client/core'; -import { NetworkManager, Manager, State, Controller, DevToolsManager, DevToolsConfig, SubscriptionManager, EndpointInterface as EndpointInterface$1, FetchFunction as FetchFunction$1, Schema as Schema$1, ResolveType as ResolveType$1, Denormalize as Denormalize$1, DenormalizeNullable as DenormalizeNullable$1, Queryable as Queryable$1, NI, SchemaArgs, NetworkError as NetworkError$1, UnknownError as UnknownError$1, ErrorTypes as ErrorTypes$2, __INTERNAL__, createReducer, applyManager, actions } from '@data-client/core'; +import { NetworkManager, Manager, State, Controller, DevToolsManager, DevToolsConfig, SubscriptionManager, EndpointInterface, FetchFunction, Schema, ResolveType, Denormalize, DenormalizeNullable, Queryable, NI, SchemaArgs, NetworkError, UnknownError, ErrorTypes as ErrorTypes$1, __INTERNAL__, createReducer, applyManager, actions } from '@data-client/core'; export { AbstractInstanceType, ActionTypes, Controller, DataClientDispatch, DefaultConnectionListener, Denormalize, DenormalizeNullable, DevToolsManager, Dispatch, EndpointExtraOptions, EndpointInterface, EntityInterface, ErrorTypes, ExpiryStatus, FetchAction, FetchFunction, GenericDispatch, InvalidateAction, LogoutManager, Manager, Middleware, MiddlewareAPI, NetworkError, NetworkManager, Normalize, NormalizeNullable, PK, PollingSubscription, Queryable, ResetAction, ResolveType, Schema, SchemaArgs, SchemaClass, SetAction, SetResponseAction, State, SubscribeAction, SubscriptionManager, UnknownError, UnsubscribeAction, UpdateFunction, actionTypes } from '@data-client/core'; import * as react_jsx_runtime from 'react/jsx-runtime'; import React, { JSX, Context } from 'react'; @@ -123,8 +123,8 @@ interface Props { * @throws {Promise} If data is not yet available. * @throws {NetworkError} If fetch fails. */ -declare function useSuspense>(endpoint: E, ...args: readonly [...Parameters]): E['schema'] extends undefined | null ? ResolveType$1 : Denormalize$1; -declare function useSuspense>(endpoint: E, ...args: readonly [...Parameters] | readonly [null]): E['schema'] extends undefined | null ? ResolveType$1 | undefined : DenormalizeNullable$1; +declare function useSuspense>(endpoint: E, ...args: readonly [...Parameters]): E['schema'] extends undefined | null ? ResolveType : Denormalize; +declare function useSuspense>(endpoint: E, ...args: readonly [...Parameters] | readonly [null]): E['schema'] extends undefined | null ? ResolveType | undefined : DenormalizeNullable; /** * Read an Endpoint's response if it is ready. @@ -132,7 +132,7 @@ declare function useSuspense, 'key' | 'schema' | 'invalidIfStale'>>(endpoint: E, ...args: readonly [...Parameters] | readonly [null]): E['schema'] extends undefined | null ? E extends (...args: any) => any ? ResolveType$1 | undefined : any : DenormalizeNullable$1; +declare function useCache, 'key' | 'schema' | 'invalidIfStale'>>(endpoint: E, ...args: readonly [...Parameters] | readonly [null]): E['schema'] extends undefined | null ? E extends (...args: any) => any ? ResolveType | undefined : any : DenormalizeNullable; /** * Query the store. @@ -140,43 +140,43 @@ declare function useCache(schema: S, ...args: NI>): DenormalizeNullable$1 | undefined; +declare function useQuery(schema: S, ...args: NI>): DenormalizeNullable | undefined; -type ErrorTypes$1 = NetworkError$1 | UnknownError$1; +type ErrorTypes = NetworkError | UnknownError; /** * Get any errors for a given request * @see https://dataclient.io/docs/api/useError */ -declare function useError>(endpoint: E, ...args: readonly [...Parameters] | readonly [null]): ErrorTypes$1 | undefined; +declare function useError>(endpoint: E, ...args: readonly [...Parameters] | readonly [null]): ErrorTypes | undefined; /** * Fetch an Endpoint if it is not in cache or stale. * @see https://dataclient.io/docs/api/useFetch */ -declare function useFetch>(endpoint: E, ...args: readonly [...Parameters]): E['schema'] extends undefined | null ? ReturnType : Promise>; -declare function useFetch>(endpoint: E, ...args: readonly [...Parameters] | readonly [null]): E['schema'] extends undefined | null ? ReturnType | undefined : Promise>; +declare function useFetch>(endpoint: E, ...args: readonly [...Parameters]): E['schema'] extends undefined | null ? ReturnType : Promise>; +declare function useFetch>(endpoint: E, ...args: readonly [...Parameters] | readonly [null]): E['schema'] extends undefined | null ? ReturnType | undefined : Promise>; /** * Keeps a resource fresh by subscribing to updates. * @see https://dataclient.io/docs/api/useSubscription */ -declare function useSubscription>(endpoint: E, ...args: readonly [...Parameters] | readonly [null]): void; +declare function useSubscription>(endpoint: E, ...args: readonly [...Parameters] | readonly [null]): void; -type SchemaReturn = { - data: Denormalize$1; +type SchemaReturn = { + data: Denormalize; loading: false; error: undefined; } | { - data: DenormalizeNullable$1; + data: DenormalizeNullable; loading: true; error: undefined; } | { - data: DenormalizeNullable$1; + data: DenormalizeNullable; loading: false; - error: ErrorTypes$2; + error: ErrorTypes$1; }; type AsyncReturn = { - data: E extends (...args: any) => any ? ResolveType$1 : any; + data: E extends (...args: any) => any ? ResolveType : any; loading: false; error: undefined; } | { @@ -186,17 +186,17 @@ type AsyncReturn = { } | { data: undefined; loading: false; - error: ErrorTypes$2; + error: ErrorTypes$1; }; /** * Use async data with { data, loading, error } (DLE) * @see https://dataclient.io/docs/api/useDLE */ -declare function useDLE>(endpoint: E, ...args: readonly [...Parameters]): E['schema'] extends undefined | null ? AsyncReturn : SchemaReturn; -declare function useDLE>(endpoint: E, ...args: readonly [...Parameters] | readonly [null]): { - data: E['schema'] extends undefined | null ? undefined : DenormalizeNullable$1; +declare function useDLE>(endpoint: E, ...args: readonly [...Parameters]): E['schema'] extends undefined | null ? AsyncReturn : SchemaReturn; +declare function useDLE>(endpoint: E, ...args: readonly [...Parameters] | readonly [null]): { + data: E['schema'] extends undefined | null ? undefined : DenormalizeNullable; loading: boolean; - error: ErrorTypes$2 | undefined; + error: ErrorTypes$1 | undefined; }; /** @@ -213,8 +213,8 @@ declare function useController(): Controller; * @throws {Promise} If data is not yet available. * @throws {NetworkError} If fetch fails. */ -declare function useLive>(endpoint: E, ...args: readonly [...Parameters]): E['schema'] extends undefined | null ? ResolveType$1 : Denormalize$1; -declare function useLive>(endpoint: E, ...args: readonly [...Parameters] | readonly [null]): E['schema'] extends undefined | null ? ResolveType$1 | undefined : DenormalizeNullable$1; +declare function useLive>(endpoint: E, ...args: readonly [...Parameters]): E['schema'] extends undefined | null ? ResolveType : Denormalize; +declare function useLive>(endpoint: E, ...args: readonly [...Parameters] | readonly [null]): E['schema'] extends undefined | null ? ResolveType | undefined : DenormalizeNullable; /** * Keeps value updated after delay time @@ -231,139 +231,6 @@ declare function useLive(value: T, delay: number, updatable?: boolean): T; -type Schema = null | string | { - [K: string]: any; -} | Schema[] | SchemaSimple | Serializable; -interface Queryable { - queryKey(args: Args, queryKey: (...args: any) => any, getEntity: GetEntity, getIndex: GetIndex): {}; -} -type Serializable = (value: any) => T; -interface SchemaSimple { - normalize(input: any, parent: any, key: any, args: any[], visit: (...args: any) => any, addEntity: (...args: any) => any, getEntity: (...args: any) => any, checkLoop: (...args: any) => any): any; - denormalize(input: {}, args: readonly any[], unvisit: (schema: any, input: any) => any): T; - queryKey(args: Args, queryKey: (...args: any) => any, getEntity: GetEntity, getIndex: GetIndex): any; -} -interface EntityInterface extends SchemaSimple { - createIfValid(props: any): any; - pk(params: any, parent?: any, key?: string, args?: readonly any[]): string | number | undefined; - readonly key: string; - merge(existing: any, incoming: any): any; - mergeWithStore(existingMeta: any, incomingMeta: any, existing: any, incoming: any): any; - mergeMetaWithStore(existingMeta: any, incomingMeta: any, existing: any, incoming: any): any; - indexes?: any; - schema: Record; - cacheWith?: object; - prototype: T; -} -/** Get Array of entities with map function applied */ -interface GetEntity { - (entityKey: string | symbol): { - readonly [pk: string]: any; - } | undefined; - (entityKey: string | symbol, pk: string | number): any; -} -/** Get PK using an Entity Index */ -interface GetIndex { - /** getIndex('User', 'username', 'ntucker') */ - (entityKey: string, field: string, value: string): { - readonly [indexKey: string]: string | undefined; - }; -} - -type AbstractInstanceType = T extends new (...args: any) => infer U ? U : T extends { - prototype: infer U; -} ? U : never; -type DenormalizeObject> = { - [K in keyof S]: S[K] extends Schema ? Denormalize : S[K]; -}; -type DenormalizeNullableObject> = { - [K in keyof S]: S[K] extends Schema ? DenormalizeNullable : S[K]; -}; -interface NestedSchemaClass { - schema: Record; - prototype: T; -} -interface RecordClass extends NestedSchemaClass { - fromJS: (...args: any) => AbstractInstanceType; -} -type DenormalizeNullableNestedSchema = keyof S['schema'] extends never ? S['prototype'] : string extends keyof S['schema'] ? S['prototype'] : S['prototype']; -type Denormalize = S extends EntityInterface ? U : S extends RecordClass ? AbstractInstanceType : S extends { - denormalize: (...args: any) => any; -} ? ReturnType : S extends Serializable ? T : S extends Array ? Denormalize[] : S extends { - [K: string]: any; -} ? DenormalizeObject : S; -type DenormalizeNullable = S extends EntityInterface ? DenormalizeNullableNestedSchema | undefined : S extends RecordClass ? DenormalizeNullableNestedSchema : S extends { - _denormalizeNullable: (...args: any) => any; -} ? ReturnType : S extends Serializable ? T : S extends Array ? Denormalize[] | undefined : S extends { - [K: string]: any; -} ? DenormalizeNullableObject : S; - -interface NetworkError extends Error { - status: number; - response?: Response; -} -interface UnknownError extends Error { - status?: unknown; - response?: unknown; -} -type ErrorTypes = NetworkError | UnknownError; - -/** What the function's promise resolves to */ -type ResolveType any> = ReturnType extends Promise ? R : never; - -type ExpiryStatusInterface = 1 | 2 | 3; - -interface SnapshotInterface { - /** - * Gets the (globally referentially stable) response for a given endpoint/args pair from state given. - * @see https://dataclient.io/docs/api/Snapshot#getResponse - */ - getResponse>(endpoint: E, ...args: readonly any[]): { - data: DenormalizeNullable; - expiryStatus: ExpiryStatusInterface; - expiresAt: number; - }; - /** @see https://dataclient.io/docs/api/Snapshot#getError */ - getError: , Args extends readonly [...Parameters]>(endpoint: E, ...args: Args) => ErrorTypes | undefined; - /** - * Retrieved memoized value for any Querable schema - * @see https://dataclient.io/docs/api/Snapshot#get - */ - get(schema: S, ...args: readonly any[]): any; - readonly fetchedAt: number; - readonly abort: Error; -} - -/** Defines a networking endpoint */ -interface EndpointInterface extends EndpointExtraOptions { - (...args: Parameters): ReturnType; - key(...args: Parameters): string; - readonly sideEffect?: M; - readonly schema?: S; -} -interface EndpointExtraOptions { - /** Default data expiry length, will fall back to NetworkManager default if not defined */ - readonly dataExpiryLength?: number; - /** Default error expiry length, will fall back to NetworkManager default if not defined */ - readonly errorExpiryLength?: number; - /** Poll with at least this frequency in miliseconds */ - readonly pollFrequency?: number; - /** Marks cached resources as invalid if they are stale */ - readonly invalidIfStale?: boolean; - /** Enables optimistic updates for this request - uses return value as assumed network response */ - getOptimisticResponse?(snap: SnapshotInterface, ...args: Parameters): ResolveType; - /** Determines whether to throw or fallback to */ - errorPolicy?(error: any): 'hard' | 'soft' | undefined; - /** User-land extra data to send */ - readonly extra?: any; -} - -type FetchFunction = (...args: A) => Promise; - /** * Builds an Endpoint that cancels fetch everytime params change * diff --git a/yarn.lock b/yarn.lock index e6244126f59c..8092e25c2a91 100644 --- a/yarn.lock +++ b/yarn.lock @@ -27022,6 +27022,7 @@ __metadata: "@changesets/changelog-github": "npm:^0.5.0" "@changesets/cli": "npm:^2.26.1" "@data-client/react": "workspace:^" + "@data-client/rest": "workspace:*" "@data-client/test": "workspace:^" "@js-temporal/polyfill": "npm:^0.4.4" "@react-navigation/native": "npm:^7.0.0"