Skip to content

feat(next): use react-query #320

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 4 commits into from
Aug 17, 2022
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 .babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
"@babel/plugin-proposal-class-properties"
],
"presets": [
"@babel/preset-env"
["@babel/preset-env", { "modules": false }]
]
}
2 changes: 1 addition & 1 deletion .eslintrc.js → .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module.exports = {
'node': true,
'browser': true
},
'parser': 'babel-eslint',
'parser': '@babel/eslint-parser',
'parserOptions': {
'ecmaVersion': 7,
'sourceType': 'module'
Expand Down
17 changes: 13 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,28 @@ jobs:
build:
strategy:
matrix:
node-version: ['14', '15']
node-version: ['lts/*', 'current']

runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: Build Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- run: yarn install
- run: yarn build
- run: yarn test
- run: yarn lint
- run: yarn test-gen
- name: Install Playwright Browsers
run: yarn playwright install --with-deps
- run: yarn test-gen && yarn test-app
- run: yarn test-gen-env
- run: yarn test-gen-openapi3
- run: yarn check
- uses: actions/upload-artifact@v3
if: always()
with:
name: playwright-report
path: playwright-report/
retention-days: 30
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/lib/
/node_modules/
/tmp
/playwright/.cache/
/playwright-report/
/test-results/
/tmp/
4 changes: 4 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

yarn lint-staged
3 changes: 3 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
runner: "jest-light-runner",
};
46 changes: 24 additions & 22 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"src",
"templates"
],
"main": "lib/index",
"type": "module",
"exports": "./lib/index.js",
"repository": "api-platform/client-generator",
"homepage": "https://github.com/api-platform/client-generator",
"bugs": "https://github.com/api-platform/client-generator/issues",
Expand All @@ -18,35 +19,40 @@
"devDependencies": {
"@babel/cli": "^7.0.0",
"@babel/core": "^7.0.0",
"@babel/eslint-parser": "^7.0.0",
"@babel/plugin-proposal-class-properties": "^7.0.0",
"@babel/plugin-proposal-export-default-from": "^7.0.0",
"@babel/plugin-transform-flow-strip-types": "^7.0.0",
"@babel/plugin-transform-runtime": "^7.0.0",
"@babel/preset-env": "^7.6.0",
"babel-eslint": "^10.0.0",
"babel-jest": "^26.0.0",
"eslint": "^7.17.0",
"eslint-config-prettier": "^7.1.0",
"eslint-plugin-import": "^2.14.0",
"eslint-plugin-prettier": "^3.3.0",
"husky": "^4.3.6",
"jest": "^26.6.3",
"lint-staged": "^10.5.3",
"@playwright-testing-library/test": "4.3.0-beta.1",
"@playwright/test": "^1.25.0",
"babel-jest": "^28.1.0",
"eslint": "^8.22.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-prettier": "^4.2.0",
"husky": "^8.0.0",
"jest": "^28.1.0",
"jest-light-runner": "^0.3.0",
"lint-staged": "^13.0.0",
"start-server-and-test": "^1.14.0",
"tmp": "^0.2.1"
},
"dependencies": {
"@api-platform/api-doc-parser": "^0.12.0",
"@api-platform/api-doc-parser": "^0.15.0",
"@babel/runtime": "^7.0.0",
"chalk": "^4.1.0",
"commander": "^8.2.0",
"chalk": "^5.0.0",
"commander": "^9.4.0",
"handlebars": "^4.0.12",
"handlebars-helpers": "^0.10.0",
"isomorphic-fetch": "^3.0.0",
"mkdirp": "^1.0.4",
"prettier": "^2.2.1",
"prettier": "^2.7.0",
"sprintf-js": "^1.1.1"
},
"scripts": {
"prepare": "husky install",
"test": "jest src",
"lint": "eslint src",
"fix": "eslint --fix src",
Expand All @@ -55,16 +61,12 @@
"watch": "babel --watch src -d lib --ignore '*.test.js'",
"test-gen": "rm -rf ./tmp && yarn build && ./lib/index.js https://demo.api-platform.com ./tmp/react -g react && ./lib/index.js https://demo.api-platform.com ./tmp/react-native -g react-native && ./lib/index.js https://demo.api-platform.com ./tmp/next -g next && ./lib/index.js https://demo.api-platform.com ./tmp/vue -g vue",
"test-gen-custom": "rm -rf ./tmp && yarn build && babel src/generators/ReactGenerator.js src/generators/BaseGenerator.js -d ./tmp/gens && cp -r ./templates/react ./templates/react-common ./templates/entrypoint.js ./tmp/gens && ./lib/index.js https://demo.api-platform.com ./tmp/react-custom -g \"$(pwd)/tmp/gens/ReactGenerator.js\" -t ./tmp/gens",
"test-gen-swagger": "rm -rf ./tmp && yarn build && ./lib/index.js https://demo.api-platform.com/docs.json ./tmp/react -f swagger && ./lib/index.js https://demo.api-platform.com/docs.json ./tmp/react-native -g react-native -f swagger && ./lib/index.js https://demo.api-platform.com/docs.json ./tmp/vue -g vue -f swagger",
"test-gen-env": "rm -rf ./tmp && yarn build && API_PLATFORM_CLIENT_GENERATOR_ENTRYPOINT=https://demo.api-platform.com API_PLATFORM_CLIENT_GENERATOR_OUTPUT=./tmp ./lib/index.js"
},
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
"test-gen-openapi3": "rm -rf ./tmp && yarn build && ./lib/index.js https://demo.api-platform.com/docs.json ./tmp/react -f openapi3 && ./lib/index.js https://demo.api-platform.com/docs.json ./tmp/react-native -g react-native -f openapi3 && ./lib/index.js https://demo.api-platform.com/docs.json ./tmp/vue -g vue -f openapi3",
"test-gen-env": "rm -rf ./tmp && yarn build && API_PLATFORM_CLIENT_GENERATOR_ENTRYPOINT=https://demo.api-platform.com API_PLATFORM_CLIENT_GENERATOR_OUTPUT=./tmp ./lib/index.js",
"test-app": "rm -rf ./tmp/app && mkdir -p ./tmp/app && yarn create next-app --typescript ./tmp/app/next && yarn --cwd ./tmp/app/next add lodash.get lodash.has isomorphic-unfetch formik react-query && yarn --cwd ./tmp/app/next add -D @types/lodash && cp -R ./tmp/next/* ./tmp/app/next && rm ./tmp/app/next/pages/index.tsx && rm -rf ./tmp/app/next/pages/api && yarn --cwd ./tmp/app/next build && start-server-and-test 'yarn --cwd ./tmp/app/next start' http://127.0.0.1:3000/books 'yarn playwright test'"
},
"lint-staged": {
"src/**/*.js": "npm run lint"
"src/**/*.js": "yarn lint --fix"
},
"bin": {
"generate-api-platform-client": "./lib/index.js"
Expand Down
107 changes: 107 additions & 0 deletions playwright.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import type { PlaywrightTestConfig } from '@playwright/test';
import { devices } from '@playwright/test';

/**
* Read environment variables from file.
* https://github.com/motdotla/dotenv
*/
// require('dotenv').config();

/**
* See https://playwright.dev/docs/test-configuration.
*/
const config: PlaywrightTestConfig = {
testDir: './tests',
/* Maximum time one test can run for. */
timeout: 30 * 1000,
expect: {
/**
* Maximum time expect() should wait for the condition to be met.
* For example in `await expect(locator).toHaveText();`
*/
timeout: 5000
},
/* Run tests in files in parallel */
fullyParallel: true,
/* Fail the build on CI if you accidentally left test.only in the source code. */
forbidOnly: !!process.env.CI,
/* Retry on CI only */
retries: process.env.CI ? 2 : 0,
/* Opt out of parallel tests on CI. */
workers: process.env.CI ? 1 : undefined,
/* Reporter to use. See https://playwright.dev/docs/test-reporters */
reporter: 'html',
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
use: {
/* Maximum time each action such as `click()` can take. Defaults to 0 (no limit). */
actionTimeout: 0,
/* Base URL to use in actions like `await page.goto('/')`. */
// baseURL: 'http://localhost:3000',

/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
trace: 'on-first-retry',
},

/* Configure projects for major browsers */
projects: [
{
name: 'chromium',
use: {
...devices['Desktop Chrome'],
},
},

{
name: 'firefox',
use: {
...devices['Desktop Firefox'],
},
},

{
name: 'webkit',
use: {
...devices['Desktop Safari'],
},
},

/* Test against mobile viewports. */
// {
// name: 'Mobile Chrome',
// use: {
// ...devices['Pixel 5'],
// },
// },
// {
// name: 'Mobile Safari',
// use: {
// ...devices['iPhone 12'],
// },
// },

/* Test against branded browsers. */
// {
// name: 'Microsoft Edge',
// use: {
// channel: 'msedge',
// },
// },
// {
// name: 'Google Chrome',
// use: {
// channel: 'chrome',
// },
// },
],

/* Folder for test artifacts such as screenshots, videos, traces, etc. */
// outputDir: 'test-results/',

/* Run your local dev server before starting the tests */
// webServer: {
// command: 'npm run start',
// port: 3000,
// },
};

export default config;
16 changes: 8 additions & 8 deletions src/generators.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import fs from "fs";
import NextGenerator from "./generators/NextGenerator";
import NuxtGenerator from "./generators/NuxtGenerator";
import ReactGenerator from "./generators/ReactGenerator";
import ReactNativeGenerator from "./generators/ReactNativeGenerator";
import TypescriptInterfaceGenerator from "./generators/TypescriptInterfaceGenerator";
import VueGenerator from "./generators/VueGenerator";
import VuetifyGenerator from "./generators/VuetifyGenerator";
import QuasarGenerator from "./generators/QuasarGenerator";
import NextGenerator from "./generators/NextGenerator.js";
import NuxtGenerator from "./generators/NuxtGenerator.js";
import ReactGenerator from "./generators/ReactGenerator.js";
import ReactNativeGenerator from "./generators/ReactNativeGenerator.js";
import TypescriptInterfaceGenerator from "./generators/TypescriptInterfaceGenerator.js";
import VueGenerator from "./generators/VueGenerator.js";
import VuetifyGenerator from "./generators/VuetifyGenerator.js";
import QuasarGenerator from "./generators/QuasarGenerator.js";

function wrap(cl) {
return ({ hydraPrefix, templateDirectory }) =>
Expand Down
10 changes: 8 additions & 2 deletions src/generators/NextGenerator.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import chalk from "chalk";
import handlebars from "handlebars";
import hbh_comparison from "handlebars-helpers/lib/comparison";
import BaseGenerator from "./BaseGenerator";
import hbh_comparison from "handlebars-helpers/lib/comparison.js";
import BaseGenerator from "./BaseGenerator.js";

export default class NextGenerator extends BaseGenerator {
constructor(params) {
Expand All @@ -10,6 +10,7 @@ export default class NextGenerator extends BaseGenerator {
this.routeAddedtoServer = false;
this.registerTemplates(`next/`, [
// components
"components/common/Layout.tsx",
"components/common/Pagination.tsx",
"components/common/ReferenceLinks.tsx",
"components/foo/List.tsx",
Expand All @@ -26,6 +27,7 @@ export default class NextGenerator extends BaseGenerator {
"pages/foos/[id]/edit.tsx",
"pages/foos/index.tsx",
"pages/foos/create.tsx",
"pages/_app.tsx",

// utils
"utils/dataAccess.ts",
Expand Down Expand Up @@ -97,13 +99,17 @@ export default class NextGenerator extends BaseGenerator {
// copy with regular name
[
// components
"components/common/Layout.tsx",
"components/common/Pagination.tsx",
"components/common/ReferenceLinks.tsx",

// types
"types/collection.ts",
"types/item.ts",

// pages
"pages/_app.tsx",

// utils
"utils/dataAccess.ts",
"utils/mercure.ts",
Expand Down
12 changes: 9 additions & 3 deletions src/generators/NextGenerator.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { Api, Resource, Field } from "@api-platform/api-doc-parser/lib";
import { Api, Resource, Field } from "@api-platform/api-doc-parser";
import path from "path";
import { fileURLToPath } from "url";
import fs from "fs";
import tmp from "tmp";
import NextGenerator from "./NextGenerator";
import NextGenerator from "./NextGenerator.js";

const dirname = path.dirname(fileURLToPath(import.meta.url));

const generator = new NextGenerator({
hydraPrefix: "hydra:",
templateDirectory: `${__dirname}/../../templates`,
templateDirectory: `${dirname}/../../templates`,
});

afterEach(() => {
Expand Down Expand Up @@ -43,6 +47,7 @@ describe("generate", () => {
"/components/abc/List.tsx",
"/components/abc/Show.tsx",
"/components/abc/Form.tsx",
"/components/common/Layout.tsx",
"/components/common/ReferenceLinks.tsx",
"/components/common/Pagination.tsx",
"/types/Abc.ts",
Expand All @@ -52,6 +57,7 @@ describe("generate", () => {
"/pages/abcs/[id]/edit.tsx",
"/pages/abcs/index.tsx",
"/pages/abcs/create.tsx",
"/pages/_app.tsx",
"/utils/dataAccess.ts",
"/utils/mercure.ts",
].forEach((file) => expect(fs.existsSync(tmpobj.name + file)).toBe(true));
Expand Down
6 changes: 3 additions & 3 deletions src/generators/NuxtGenerator.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import chalk from "chalk";
import BaseVueGenerator from "./VueBaseGenerator";
import BaseVueGenerator from "./VueBaseGenerator.js";

export default class NuxtGenerator extends BaseVueGenerator {
constructor(params) {
Expand Down Expand Up @@ -106,9 +106,9 @@ export default class NuxtGenerator extends BaseVueGenerator {

this.createEntrypoint(api.entrypoint, `${dir}/config/entrypoint.js`);

for (let dir of [`${dir}/components/${lc}`, `${dir}/pages/${lc}s`]) {
[`${dir}/components/${lc}`, `${dir}/pages/${lc}s`].forEach((dir) => {
this.createDir(dir);
}
});

this.createFile("services/api.js", `${dir}/services/api.js`, {}, false);

Expand Down
10 changes: 7 additions & 3 deletions src/generators/NuxtGenerator.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { Api, Resource, Field } from "@api-platform/api-doc-parser/lib";
import { Api, Resource, Field } from "@api-platform/api-doc-parser";
import path from "path";
import { fileURLToPath } from "url";
import fs from "fs";
import tmp from "tmp";
import NuxtGenerator from "./NuxtGenerator";
import NuxtGenerator from "./NuxtGenerator.js";

const dirname = path.dirname(fileURLToPath(import.meta.url));

const generator = new NuxtGenerator({
hydraPrefix: "hydra:",
templateDirectory: `${__dirname}/../../templates`,
templateDirectory: `${dirname}/../../templates`,
});

afterEach(() => {
Expand Down
Loading