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 package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zenstack-monorepo",
"version": "1.0.0-alpha.87",
"version": "1.0.0-alpha.96",
"description": "",
"scripts": {
"build": "pnpm -r build",
Expand Down
2 changes: 1 addition & 1 deletion packages/language/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zenstackhq/language",
"version": "1.0.0-alpha.87",
"version": "1.0.0-alpha.96",
"displayName": "ZenStack modeling language compiler",
"description": "ZenStack modeling language compiler",
"homepage": "https://zenstack.dev",
Expand Down
9 changes: 4 additions & 5 deletions packages/next/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zenstackhq/next",
"version": "1.0.0-alpha.87",
"version": "1.0.0-alpha.96",
"displayName": "ZenStack Next.js integration",
"description": "ZenStack Next.js integration",
"homepage": "https://zenstack.dev",
Expand All @@ -21,12 +21,10 @@
"author": "",
"license": "MIT",
"peerDependencies": {
"next": "^12.3.1 || ^13",
"react": "^17.0.2 || ^18"
"next": "^12.3.1 || ^13"
},
"dependencies": {
"@zenstackhq/runtime": "workspace:*",
"@zenstackhq/testtools": "workspace:*",
"tmp": "^0.2.1"
},
"devDependencies": {
Expand All @@ -40,6 +38,7 @@
"superjson": "^1.11.0",
"supertest": "^6.3.3",
"ts-jest": "^29.0.5",
"typescript": "^4.9.4"
"typescript": "^4.9.4",
"@zenstackhq/testtools": "workspace:*"
}
}
2 changes: 1 addition & 1 deletion packages/plugins/openapi/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@zenstackhq/openapi",
"displayName": "ZenStack Plugin and Runtime for OpenAPI",
"version": "1.0.0-alpha.87",
"version": "1.0.0-alpha.96",
"description": "ZenStack plugin and runtime supporting OpenAPI",
"main": "index.js",
"repository": {
Expand Down
17 changes: 9 additions & 8 deletions packages/plugins/openapi/src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export class OpenAPIGenerator {
this.pruneComponents(components);

const openapi: OAPI.Document = {
openapi: '3.1.0',
openapi: this.getOption('specVersion', '3.1.0'),
info: {
title: this.getOption('title', 'ZenStack Generated API'),
version: this.getOption('version', '1.0.0'),
Expand Down Expand Up @@ -180,6 +180,7 @@ export class OpenAPIGenerator {
};

const definitions: OperationDefinition[] = [];
const hasRelation = zmodel.fields.some((f) => isDataModel(f.$resolvedType?.decl));

if (ops['createOne']) {
definitions.push({
Expand All @@ -191,7 +192,7 @@ export class OpenAPIGenerator {
type: 'object',
properties: {
select: this.ref(`${model.name}Select`),
include: this.ref(`${model.name}Include`),
include: hasRelation ? this.ref(`${model.name}Include`) : undefined,
data: this.ref(`${model.name}CreateInput`),
},
},
Expand Down Expand Up @@ -233,7 +234,7 @@ export class OpenAPIGenerator {
type: 'object',
properties: {
select: this.ref(`${model.name}Select`),
include: this.ref(`${model.name}Include`),
include: hasRelation ? this.ref(`${model.name}Include`) : undefined,
where: this.ref(`${model.name}WhereUniqueInput`),
},
},
Expand All @@ -254,7 +255,7 @@ export class OpenAPIGenerator {
type: 'object',
properties: {
select: this.ref(`${model.name}Select`),
include: this.ref(`${model.name}Include`),
include: hasRelation ? this.ref(`${model.name}Include`) : undefined,
where: this.ref(`${model.name}WhereInput`),
},
},
Expand All @@ -275,7 +276,7 @@ export class OpenAPIGenerator {
type: 'object',
properties: {
select: this.ref(`${model.name}Select`),
include: this.ref(`${model.name}Include`),
include: hasRelation ? this.ref(`${model.name}Include`) : undefined,
where: this.ref(`${model.name}WhereInput`),
},
},
Expand All @@ -296,7 +297,7 @@ export class OpenAPIGenerator {
type: 'object',
properties: {
select: this.ref(`${model.name}Select`),
include: this.ref(`${model.name}Include`),
include: hasRelation ? this.ref(`${model.name}Include`) : undefined,
where: this.ref(`${model.name}WhereUniqueInput`),
data: this.ref(`${model.name}UpdateInput`),
},
Expand Down Expand Up @@ -338,7 +339,7 @@ export class OpenAPIGenerator {
type: 'object',
properties: {
select: this.ref(`${model.name}Select`),
include: this.ref(`${model.name}Include`),
include: hasRelation ? this.ref(`${model.name}Include`) : undefined,
where: this.ref(`${model.name}WhereUniqueInput`),
create: this.ref(`${model.name}CreateInput`),
update: this.ref(`${model.name}UpdateInput`),
Expand All @@ -361,7 +362,7 @@ export class OpenAPIGenerator {
type: 'object',
properties: {
select: this.ref(`${model.name}Select`),
include: this.ref(`${model.name}Include`),
include: hasRelation ? this.ref(`${model.name}Include`) : undefined,
where: this.ref(`${model.name}WhereUniqueInput`),
},
},
Expand Down
81 changes: 80 additions & 1 deletion packages/plugins/openapi/tests/openapi.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
/// <reference types="@types/jest" />

import OpenAPIParser from '@readme/openapi-parser';
import { loadZModelAndDmmf } from '@zenstackhq/testtools';
import * as tmp from 'tmp';
import * as fs from 'fs';
import generate from '../src';
import YAML from 'yaml';
import { isPlugin, Model, Plugin } from '@zenstackhq/sdk/ast';
import { getLiteral } from '@zenstackhq/sdk';

describe('Open API Plugin Tests', () => {
it('run plugin', async () => {
Expand Down Expand Up @@ -68,10 +73,15 @@ model Bar {
`);

const { name: output } = tmp.fileSync({ postfix: '.yaml' });
generate(model, { schemaPath: modelFile, output }, dmmf);

const options = buildOptions(model, modelFile, output);
generate(model, options, dmmf);

console.log('OpenAPI specification generated:', output);

const parsed = YAML.parse(fs.readFileSync(output, 'utf-8'));
expect(parsed.openapi).toBe('3.1.0');

const api = await OpenAPIParser.validate(output);
expect(api.paths?.['/user/findMany']?.['get']?.description).toBe('Find users matching the given conditions');
const del = api.paths?.['/user/dodelete']?.['put'];
Expand All @@ -83,4 +93,73 @@ model Bar {
expect(api.paths?.['/foo/findMany']).toBeUndefined();
expect(api.paths?.['/bar/findMany']).toBeUndefined();
});

it('options', async () => {
const { model, dmmf, modelFile } = await loadZModelAndDmmf(`
plugin openapi {
provider = '${process.cwd()}/dist'
specVersion = '3.0.0'
title = 'My Awesome API'
version = '1.0.0'
description = 'awesome api'
prefix = '/myapi'
}

model User {
id String @id
}
`);

const { name: output } = tmp.fileSync({ postfix: '.yaml' });
const options = buildOptions(model, modelFile, output);
generate(model, options, dmmf);

console.log('OpenAPI specification generated:', output);

const parsed = YAML.parse(fs.readFileSync(output, 'utf-8'));
expect(parsed.openapi).toBe('3.0.0');

const api = await OpenAPIParser.validate(output);
expect(api.info).toEqual(
expect.objectContaining({
title: 'My Awesome API',
version: '1.0.0',
description: 'awesome api',
})
);

expect(api.paths?.['/myapi/user/findMany']).toBeTruthy();
});

it('v3.1.0 fields', async () => {
const { model, dmmf, modelFile } = await loadZModelAndDmmf(`
plugin openapi {
provider = '${process.cwd()}/dist'
summary = 'awesome api'
}

model User {
id String @id
}
`);

const { name: output } = tmp.fileSync({ postfix: '.yaml' });
const options = buildOptions(model, modelFile, output);
generate(model, options, dmmf);

console.log('OpenAPI specification generated:', output);

const parsed = YAML.parse(fs.readFileSync(output, 'utf-8'));
expect(parsed.openapi).toBe('3.1.0');

const api = await OpenAPIParser.validate(output);
expect((api.info as any).summary).toEqual('awesome api');
});
});

function buildOptions(model: Model, modelFile: string, output: string) {
const optionFields = model.declarations.find((d): d is Plugin => isPlugin(d))?.fields || [];
const options: any = { schemaPath: modelFile, output };
optionFields.forEach((f) => (options[f.name] = getLiteral(f.value)));
return options;
}
15 changes: 10 additions & 5 deletions packages/plugins/react/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@zenstackhq/react",
"displayName": "ZenStack plugin and runtime for ReactJS",
"version": "1.0.0-alpha.87",
"version": "1.0.0-alpha.96",
"description": "ZenStack plugin and runtime for ReactJS",
"main": "index.js",
"repository": {
Expand Down Expand Up @@ -29,22 +29,27 @@
"change-case": "^4.1.2",
"decimal.js": "^10.4.2",
"superjson": "^1.11.0",
"swr": "^2.0.3",
"ts-morph": "^16.0.0"
},
"peerDependencies": {
"react": "^17.0.2 || ^18",
"react-dom": "^17.0.2 || ^18"
"react-dom": "^17.0.2 || ^18",
"swr": "2.x",
"@tanstack/react-query": "4.x"
},
"devDependencies": {
"@tanstack/react-query": "^4.28.0",
"@types/jest": "^29.5.0",
"@types/react": "^18.0.26",
"@types/tmp": "^0.2.3",
"@zenstackhq/testtools": "workspace:*",
"copyfiles": "^2.4.1",
"jest": "^29.5.0",
"rimraf": "^3.0.2",
"react": "^17.0.2 || ^18",
"react-dom": "^17.0.2 || ^18",
"swr": "^2.0.3",
"ts-jest": "^29.0.5",
"typescript": "^4.9.4",
"@zenstackhq/testtools": "workspace:*"
"typescript": "^4.9.4"
}
}
17 changes: 17 additions & 0 deletions packages/plugins/react/src/generator/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import type { DMMF } from '@prisma/generator-helper';
import { PluginError, type PluginOptions } from '@zenstackhq/sdk';
import type { Model } from '@zenstackhq/sdk/ast';
import { generate as reactQueryGenerate } from './react-query';
import { generate as swrGenerate } from './swr';

export async function generate(model: Model, options: PluginOptions, dmmf: DMMF.Document) {
const fetcher = (options.fetcher as string) ?? 'swr';
switch (fetcher) {
case 'swr':
return swrGenerate(model, options, dmmf);
case 'react-query':
return reactQueryGenerate(model, options, dmmf);
default:
throw new PluginError(`Unknown "fetcher" option: ${fetcher}, use "swr" or "react-query"`);
}
}
Loading