Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
github: [skonves]
github: [basketry]
1,242 changes: 697 additions & 545 deletions package-lock.json

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@basketry/react-query",
"version": "0.0.0",
"version": "0.2.0-rc.0",
"description": "Basketry generator for generating Typescript interfaces",
"main": "./lib/index.js",
"scripts": {
Expand Down Expand Up @@ -31,7 +31,8 @@
"bugs": {
"url": "https://github.com/basketry/react-query/issues"
},
"homepage": "https://github.com/basketry/react-query#readme",
"homepage": "https://basketry.io/docs/components/@basketry/react-query",
"funding": "https://github.com/sponsors/basketry",
"devDependencies": {
"@basketry/dotfiles": "^1.1.0",
"@types/jest": "^27.4.0",
Expand All @@ -50,8 +51,8 @@
"typescript": "^4.5.5"
},
"dependencies": {
"@basketry/typescript": "^0.1.2",
"basketry": "^0.1.2",
"@basketry/typescript": "^0.2.0",
"basketry": "^0.2.0",
"case": "^1.6.3",
"pluralize": "^8.0.0",
"prettier": "^2.5.1"
Expand Down
39 changes: 26 additions & 13 deletions src/context-file.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { camel, pascal } from 'case';
import { ModuleBuilder } from './module-builder';
import { ImportBuilder } from './import-builder';
import { NameFactory } from './name-factory';

export class ContextFile extends ModuleBuilder {
private readonly react = new ImportBuilder('react');
private readonly nameFactory = new NameFactory(this.service, this.options);
private readonly react = new ImportBuilder(
'react',
this.options?.reactQuery?.reactImport ? 'React' : undefined,
);
private readonly client = new ImportBuilder(
this.options?.reactQuery?.clientModule ?? '../http-client',
);
Expand All @@ -23,26 +28,34 @@ export class ContextFile extends ModuleBuilder {
const FetchLike = () => this.client.type('FetchLike');
const OptionsType = () => this.client.type(optionsName);

yield `export interface ClientContextProps { fetch: ${FetchLike()}; options: ${OptionsType()}; }`;
yield `const ClientContext = ${createContext()}<ClientContextProps | undefined>( undefined );`;
const contextName = this.nameFactory.buildContextName();
const contextPropsName = pascal(`${contextName}_props`);
const providerName = this.nameFactory.buildProviderName();

yield `export interface ${contextPropsName} extends ${OptionsType()} { fetch?: ${FetchLike()}; }`;
yield `const ${contextName} = ${createContext()}<${contextPropsName} | undefined>( undefined );`;
yield ``;
yield `export const ClientProvider: ${FC()}<${PropsWithChildren()}<ClientContextProps>> = ({ children, fetch, options }) => {`;
yield ` const value = ${useMemo()}(() => ({ fetch, options }), [fetch, options.mapUnhandledException, options.mapValidationError, options.root]);`;
yield ` return <ClientContext.Provider value={value}>{children}</ClientContext.Provider>;`;
yield `export const ${providerName}: ${FC()}<${PropsWithChildren()}<${contextPropsName}>> = ({ children, ...props }) => {`;
yield ` const value = ${useMemo()}(() => ({ ...props }), [props.fetch, props.mapUnhandledException, props.mapValidationError, props.root]);`;
yield ` return <${contextName}.Provider value={value}>{children}</${contextName}.Provider>;`;
yield `};`;
for (const int of this.service.interfaces) {
const hookName = camel(`use_${int.name.value}_service`);
const localName = camel(`${int.name.value}_service`);
const interfaceName = pascal(`${int.name.value}_service`);
for (const int of [...this.service.interfaces].sort((a, b) =>
a.name.value.localeCompare(b.name.value),
)) {
const hookName = this.nameFactory.buildServiceHookName(int);
const localName = this.nameFactory.buildServiceName(int);
const interfaceName = pascal(localName);
const className = pascal(`http_${int.name.value}_service`);

yield ``;
yield `export const ${hookName} = () => {`;
yield ` const context = ${useContext()}(ClientContext);`;
yield ` if (!context) { throw new Error('${hookName} must be used within a ClientProvider'); }`;
yield ` const context = ${useContext()}(${contextName});`;
yield ` if (!context) { throw new Error('${hookName} must be used within a ${providerName}'); }`;
yield ` const ${localName}: ${this.types.type(
interfaceName,
)} = new ${this.client.fn(className)}(context.fetch, context.options);`;
)} = new ${this.client.fn(
className,
)}(context.fetch ?? window.fetch.bind(window), context);`;
yield ` return ${localName};`;
yield `}`;
}
Expand Down
Loading
Loading