Skip to content

Commit 2cf0245

Browse files
committed
extract common hooks
1 parent 9f0bcf5 commit 2cf0245

File tree

151 files changed

+3823
-3632
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

151 files changed

+3823
-3632
lines changed

jest.config.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
module.exports = {
22
projects: [
33
"<rootDir>/packages/api-client-core/jest.config.js",
4+
"<rootDir>/packages/client-hooks/jest.config.js",
45
"<rootDir>/packages/core/jest.config.js",
6+
"<rootDir>/packages/preact/jest.config.js",
57
"<rootDir>/packages/react/jest.config.js",
8+
"<rootDir>/packages/react-bigcommerce/jest.config.js",
69
"<rootDir>/packages/react-shopify-app-bridge/jest.config.js",
7-
"<rootDir>/packages/tiny-graphql-query-compiler/jest.config.js",
810
"<rootDir>/packages/shopify-extensions/jest.config.js",
9-
"<rootDir>/packages/react-bigcommerce/jest.config.js",
11+
"<rootDir>/packages/tiny-graphql-query-compiler/jest.config.js",
12+
"<rootDir>/packages/utils/jest.config.js",
1013
],
1114
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"lint:eslint": "NODE_OPTIONS=\"--max-old-space-size=4096\" eslint --quiet --ext ts,tsx packages scripts",
1313
"lint:fix": "NODE_OPTIONS=\"--max-old-space-size=4096\" prettier --write --check \"(packages|scripts)/**/*.{js,ts,tsx}\" && eslint --ext ts,tsx --fix packages scripts",
1414
"typecheck": "pnpm -r --no-bail run --if-present typecheck",
15-
"build": "pnpm -r --no-bail run --if-present build",
15+
"build": "pnpm --filter=@gadgetinc/utils build && pnpm -r --no-bail run --if-present build",
1616
"prerelease": "pnpm -r --no-bail run --if-present prerelease",
1717
"watch": "run-p --print-label watch:*",
1818
"watch:client": "pnpm --filter=@gadgetinc/api-client-core watch",

packages/blog-example/src/main.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import "./styles/index.css";
77

88
ReactDOM.createRoot(document.getElementById("root") as HTMLElement).render(
99
<React.StrictMode>
10-
<Provider value={api.connection.currentClient}>
10+
<Provider api={api}>
1111
<App />
1212
</Provider>
1313
</React.StrictMode>
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
// For a detailed explanation regarding each configuration property, visit:
2+
// https://jestjs.io/docs/en/configuration.html
3+
4+
export default {
5+
displayName: "client-hooks",
6+
// All imported modules in your tests should be mocked automatically
7+
// automock: false,
8+
9+
// Stop running tests after `n` failures
10+
// bail: 0,
11+
12+
// The directory where Jest should store its cached dependency information
13+
// cacheDirectory: process.env.LAYERCI ? "/tmp/jest-cache" : undefined,
14+
15+
// Automatically clear mock calls and instances between every test
16+
clearMocks: true,
17+
18+
// Indicates whether the coverage information should be collected while executing the test
19+
// collectCoverage: false,
20+
21+
// An array of glob patterns indicating a set of files for which coverage information should be collected
22+
// collectCoverageFrom: undefined,
23+
24+
// The directory where Jest should output its coverage files
25+
// coverageDirectory: undefined,
26+
27+
// An array of regexp pattern strings used to skip coverage collection
28+
// coveragePathIgnorePatterns: [
29+
// "/node_modules/"
30+
// ],
31+
32+
// A list of reporter names that Jest uses when writing coverage reports
33+
// coverageReporters: [
34+
// "json",
35+
// "text",
36+
// "lcov",
37+
// "clover"
38+
// ],
39+
40+
// An object that configures minimum threshold enforcement for coverage results
41+
// coverageThreshold: undefined,
42+
43+
// A path to a custom dependency extractor
44+
// dependencyExtractor: undefined,
45+
46+
// Make calling deprecated APIs throw helpful error messages
47+
// errorOnDeprecated: false,
48+
49+
extensionsToTreatAsEsm: [".ts", ".tsx"],
50+
51+
// Force coverage collection from ignored files using an array of glob patterns
52+
// forceCoverageMatch: [],
53+
54+
// A path to a module which exports an async function that is triggered once before all test suites
55+
// globalSetup: "<rootDir>/../api/spec/jest.globalsetup.ts",
56+
57+
// A path to a module which exports an async function that is triggered once after all test suites
58+
// globalTeardown: undefined,
59+
60+
// A set of global variables that need to be available in all test environments
61+
// globals: {},
62+
63+
// The maximum amount of workers used to run your tests. Can be specified as % or a number. E.g. maxWorkers: 10% will use 10% of your CPU amount + 1 as the maximum worker number. maxWorkers: 2 will use a maximum of 2 workers.
64+
// maxWorkers: "50%",
65+
66+
// An array of directory names to be searched recursively up from the requiring module's location
67+
// moduleDirectories: [
68+
// "node_modules"
69+
// ],
70+
71+
// An array of file extensions your modules use
72+
// moduleFileExtensions: [
73+
// "js",
74+
// "json",
75+
// "jsx",
76+
// "ts",
77+
// "tsx",
78+
// "node"
79+
// ],
80+
81+
// A map from regular expressions to module names or to arrays of module names that allow to stub out resources with a single module
82+
moduleNameMapper: {
83+
"^(\\.{1,2}/.*)\\.js$": "$1",
84+
},
85+
86+
// An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader
87+
// modulePathIgnorePatterns: [],
88+
89+
// Activates notifications for test results
90+
// notify: false,
91+
92+
// An enum that specifies notification mode. Requires { notify: true }
93+
// notifyMode: "failure-change",
94+
95+
// A preset that is used as a base for Jest's configuration
96+
// preset: "ts-jest",
97+
98+
// Run tests from one or more projects
99+
// projects: undefined,
100+
101+
// Use this configuration option to add custom reporters to Jest
102+
// reporters: undefined,
103+
104+
// Automatically reset mock state between every test
105+
// resetMocks: false,
106+
107+
// Reset the module registry before running each individual test
108+
// resetModules: false,
109+
110+
// A path to a custom resolver
111+
// resolver: undefined,
112+
113+
// Automatically restore mock state between every test
114+
restoreMocks: true,
115+
116+
// The root directory that Jest should scan for tests and modules within
117+
// rootDir: undefined,
118+
119+
// A list of paths to directories that Jest should use to search for files in
120+
roots: ["<rootDir>"],
121+
122+
// Allows you to use a custom runner instead of Jest's default test runner
123+
// runner: "jest-runner",
124+
125+
// The paths to modules that run some code to configure or set up the testing environment before each test
126+
// setupFiles: ["./spec/setup.ts"],
127+
128+
// A list of paths to modules that run some code to configure or set up the testing framework before each test
129+
// setupFilesAfterEnv: ["<rootDir>/spec/jest.setup.ts"],
130+
131+
// A list of paths to snapshot serializer modules Jest should use for snapshot testing
132+
// snapshotSerializers: [],
133+
134+
// The test environment that will be used for testing
135+
testEnvironment: "setup-polly-jest/jest-environment-jsdom",
136+
137+
// Options that will be passed to the testEnvironment
138+
// testEnvironmentOptions: {},
139+
140+
// Adds a location field to test results
141+
// testLocationInResults: false,
142+
143+
// The glob patterns Jest uses to detect test files
144+
// testMatch: [path.join(__dirname, "spec/(*.)+(spec|test).[tj]s?(x)")],
145+
146+
// An array of regexp pattern strings that are matched against all test paths, matched tests are skipped
147+
testPathIgnorePatterns: ["/node_modules/"],
148+
149+
// The regexp pattern or array of patterns that Jest uses to detect test files
150+
// testRegex: [],
151+
152+
// This option allows the use of a custom results processor
153+
// testResultsProcessor: undefined,
154+
155+
// This option allows use of a custom test runner
156+
// testRunner: "jasmine2",
157+
testRunner: "jest-circus/runner",
158+
159+
// This option sets the URL for the jsdom environment. It is reflected in properties such as location.href
160+
// testURL: "http://localhost",
161+
162+
// Setting this value to "fake" allows the use of fake timers for functions such as "setTimeout"
163+
// timers: "real",
164+
165+
// A map from regular expressions to paths to transformers
166+
// transform: undefined,
167+
transform: { "^.+\\.(t|j)sx?$": ["@swc/jest"] },
168+
169+
// An array of regexp pattern strings that are matched against all source file paths, matched files will skip transformation
170+
// transformIgnorePatterns: [
171+
// "/node_modules/"
172+
// ],
173+
// transformIgnorePatterns: ["/node_modules/(?!lodash)"],
174+
175+
// An array of regexp pattern strings that are matched against all modules before the module loader will automatically return a mock for them
176+
// unmockedModulePathPatterns: undefined,
177+
178+
// Indicates whether each individual test should be reported during the run
179+
// verbose: undefined,
180+
181+
// An array of regexp patterns that are matched against all source file paths before re-running tests in watch mode
182+
// watchPathIgnorePatterns: [],
183+
184+
// Whether to use watchman for file crawling
185+
// watchman: true,
186+
};

packages/client-hooks/package.json

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"name": "@gadgetinc/client-hooks",
3+
"version": "0.1.0",
4+
"files": [
5+
"README.md",
6+
"dist/**/*"
7+
],
8+
"license": "MIT",
9+
"repository": "github:gadget-inc/js-clients",
10+
"homepage": "https://github.com/gadget-inc/js-clients/tree/main/packages/client-hooks",
11+
"type": "module",
12+
"exports": {
13+
"./package.json": "./package.json",
14+
".": {
15+
"import": "./dist/esm/index.js",
16+
"require": "./dist/cjs/index.js",
17+
"default": "./dist/esm/index.js"
18+
}
19+
},
20+
"source": "src/index.ts",
21+
"main": "dist/cjs/index.js",
22+
"sideEffects": false,
23+
"scripts": {
24+
"typecheck:main": "tsc --noEmit",
25+
"typecheck": "tsc --noEmit",
26+
"clean": "rimraf dist/ *.tsbuildinfo **/*.tsbuildinfo",
27+
"prebuild": "mkdir -p dist/cjs dist/esm && echo '{\"type\": \"commonjs\"}' > dist/cjs/package.json && echo '{\"type\": \"module\"}' > dist/esm/package.json",
28+
"build": "pnpm clean && pnpm prebuild && tsc -b tsconfig.cjs.json tsconfig.esm.json",
29+
"prepublishOnly": "pnpm build",
30+
"prerelease": "gitpkg publish"
31+
},
32+
"dependencies": {
33+
"@gadgetinc/utils": "^0.1.0"
34+
},
35+
"devDependencies": {
36+
"@gadgetinc/utils": "workspace:*",
37+
"@gadgetinc/api-client-core": "workspace:*",
38+
"@urql/core": "*"
39+
}
40+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import type { AnyClient, GadgetConnection } from "@gadgetinc/api-client-core";
2+
import type {
3+
AnyVariables,
4+
Client,
5+
CombinedError,
6+
DocumentInput,
7+
GraphQLRequestParams,
8+
Operation,
9+
OperationContext,
10+
OperationResult,
11+
RequestPolicy,
12+
} from "@urql/core";
13+
14+
type Dispose = void | (() => void);
15+
type AnyActionArg = [] | [any];
16+
type ActionDispatch<ActionArg extends AnyActionArg> = (...args: ActionArg) => void;
17+
18+
type Context<_T> = {
19+
Provider: unknown;
20+
Consumer: unknown;
21+
};
22+
23+
interface FrameworkBindings {
24+
deepEqual: <A, B>(a: A, b: B) => boolean;
25+
useEffect: (fn: () => Dispose, deps?: any[]) => void;
26+
useMemo: <T>(factory: () => T, deps: any[]) => T;
27+
useRef: <T>(initial: T) => { current: T };
28+
useState: <T>(initial: T | (() => T)) => [T, (next: T) => void];
29+
useContext: <T>(ctx: any) => T;
30+
createContext: <T>(defaultValue: T) => Context<T>;
31+
useCallback: <T extends (...args: any[]) => any>(fn: T, deps: any[]) => T;
32+
useReducer: <S, I, A extends AnyActionArg>(
33+
reducer: (prevState: S, ...args: A) => S,
34+
initialArg: I,
35+
init?: (i: I) => S
36+
) => [S, ActionDispatch<A>];
37+
Fragment: unknown;
38+
}
39+
40+
export type UseQueryArgs<Variables extends AnyVariables = AnyVariables, Data = any> = {
41+
requestPolicy?: RequestPolicy;
42+
context?: Partial<OperationContext>;
43+
pause?: boolean;
44+
} & GraphQLRequestParams<Data, Variables>;
45+
46+
export interface UseQueryState<Data = any, Variables extends AnyVariables = AnyVariables> {
47+
fetching: boolean;
48+
stale: boolean;
49+
data?: Data;
50+
error?: CombinedError;
51+
extensions?: Record<string, any>;
52+
operation?: Operation<Data, Variables>;
53+
}
54+
55+
type UseQueryExecute = (opts?: Partial<OperationContext>) => void;
56+
57+
export type UseQueryResponse<Data = any, Variables extends AnyVariables = AnyVariables> = [UseQueryState<Data, Variables>, UseQueryExecute];
58+
59+
export interface UseMutationState<Data = any, Variables extends AnyVariables = AnyVariables> {
60+
fetching: boolean;
61+
stale: boolean;
62+
data?: Data;
63+
error?: CombinedError;
64+
extensions?: Record<string, any>;
65+
operation?: Operation<Data, Variables>;
66+
}
67+
68+
type UseMutationExecute<Data = any, Variables extends AnyVariables = AnyVariables> = (
69+
variables: Variables,
70+
context?: Partial<OperationContext>
71+
) => Promise<OperationResult<Data, Variables>>;
72+
73+
export type UseMutationResponse<Data = any, Variables extends AnyVariables = AnyVariables> = [
74+
UseMutationState<Data, Variables>,
75+
UseMutationExecute<Data, Variables>
76+
];
77+
78+
interface UrqlBindings {
79+
Provider: (props: { client: Client; children: any }) => any;
80+
useQuery: <Data = any, Variables extends AnyVariables = AnyVariables>(
81+
args: UseQueryArgs<Variables, Data>
82+
) => UseQueryResponse<Data, Variables>;
83+
useMutation: <Data = any, Variables extends AnyVariables = AnyVariables>(
84+
query: DocumentInput<Data, Variables>
85+
) => UseMutationResponse<Data, Variables>;
86+
}
87+
88+
export interface GadgetApiContext {
89+
api: AnyClient;
90+
connection: GadgetConnection;
91+
}
92+
93+
export interface RuntimeAdapter {
94+
GadgetApiContext: Context<GadgetApiContext>;
95+
framework: FrameworkBindings;
96+
urql: UrqlBindings;
97+
}

0 commit comments

Comments
 (0)