Skip to content

Commit 0a78c78

Browse files
feat(next): use react-query (#320)
* ci: build next app * feat: adding react query to fetch data * chore: upgrade deps * test: add e2e tests Co-authored-by: Alan Poulain <[email protected]>
1 parent 4ab98db commit 0a78c78

Some content is hidden

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

47 files changed

+3513
-3427
lines changed

.babelrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
"@babel/plugin-proposal-class-properties"
77
],
88
"presets": [
9-
"@babel/preset-env"
9+
["@babel/preset-env", { "modules": false }]
1010
]
1111
}

.eslintrc.js renamed to .eslintrc.cjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module.exports = {
55
'node': true,
66
'browser': true
77
},
8-
'parser': 'babel-eslint',
8+
'parser': '@babel/eslint-parser',
99
'parserOptions': {
1010
'ecmaVersion': 7,
1111
'sourceType': 'module'

.github/workflows/ci.yml

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,28 @@ jobs:
99
build:
1010
strategy:
1111
matrix:
12-
node-version: ['14', '15']
12+
node-version: ['lts/*', 'current']
1313

1414
runs-on: ubuntu-latest
1515
steps:
16-
- uses: actions/checkout@v2
16+
- uses: actions/checkout@v3
1717
- name: Build Node.js ${{ matrix.node-version }}
18-
uses: actions/setup-node@v1
18+
uses: actions/setup-node@v3
1919
with:
2020
node-version: ${{ matrix.node-version }}
2121
- run: yarn install
2222
- run: yarn build
2323
- run: yarn test
2424
- run: yarn lint
25-
- run: yarn test-gen
25+
- name: Install Playwright Browsers
26+
run: yarn playwright install --with-deps
27+
- run: yarn test-gen && yarn test-app
2628
- run: yarn test-gen-env
29+
- run: yarn test-gen-openapi3
2730
- run: yarn check
31+
- uses: actions/upload-artifact@v3
32+
if: always()
33+
with:
34+
name: playwright-report
35+
path: playwright-report/
36+
retention-days: 30

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
/lib/
22
/node_modules/
3-
/tmp
3+
/playwright/.cache/
4+
/playwright-report/
5+
/test-results/
6+
/tmp/

.husky/pre-commit

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/usr/bin/env sh
2+
. "$(dirname -- "$0")/_/husky.sh"
3+
4+
yarn lint-staged

jest.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default {
2+
runner: "jest-light-runner",
3+
};

package.json

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
"src",
1010
"templates"
1111
],
12-
"main": "lib/index",
12+
"type": "module",
13+
"exports": "./lib/index.js",
1314
"repository": "api-platform/client-generator",
1415
"homepage": "https://github.com/api-platform/client-generator",
1516
"bugs": "https://github.com/api-platform/client-generator/issues",
@@ -18,35 +19,40 @@
1819
"devDependencies": {
1920
"@babel/cli": "^7.0.0",
2021
"@babel/core": "^7.0.0",
22+
"@babel/eslint-parser": "^7.0.0",
2123
"@babel/plugin-proposal-class-properties": "^7.0.0",
2224
"@babel/plugin-proposal-export-default-from": "^7.0.0",
2325
"@babel/plugin-transform-flow-strip-types": "^7.0.0",
2426
"@babel/plugin-transform-runtime": "^7.0.0",
2527
"@babel/preset-env": "^7.6.0",
26-
"babel-eslint": "^10.0.0",
27-
"babel-jest": "^26.0.0",
28-
"eslint": "^7.17.0",
29-
"eslint-config-prettier": "^7.1.0",
30-
"eslint-plugin-import": "^2.14.0",
31-
"eslint-plugin-prettier": "^3.3.0",
32-
"husky": "^4.3.6",
33-
"jest": "^26.6.3",
34-
"lint-staged": "^10.5.3",
28+
"@playwright-testing-library/test": "4.3.0-beta.1",
29+
"@playwright/test": "^1.25.0",
30+
"babel-jest": "^28.1.0",
31+
"eslint": "^8.22.0",
32+
"eslint-config-prettier": "^8.5.0",
33+
"eslint-plugin-import": "^2.26.0",
34+
"eslint-plugin-prettier": "^4.2.0",
35+
"husky": "^8.0.0",
36+
"jest": "^28.1.0",
37+
"jest-light-runner": "^0.3.0",
38+
"lint-staged": "^13.0.0",
39+
"start-server-and-test": "^1.14.0",
3540
"tmp": "^0.2.1"
3641
},
3742
"dependencies": {
38-
"@api-platform/api-doc-parser": "^0.12.0",
43+
"@api-platform/api-doc-parser": "^0.15.0",
3944
"@babel/runtime": "^7.0.0",
40-
"chalk": "^4.1.0",
41-
"commander": "^8.2.0",
45+
"chalk": "^5.0.0",
46+
"commander": "^9.4.0",
4247
"handlebars": "^4.0.12",
4348
"handlebars-helpers": "^0.10.0",
4449
"isomorphic-fetch": "^3.0.0",
4550
"mkdirp": "^1.0.4",
46-
"prettier": "^2.2.1",
51+
"prettier": "^2.7.0",
4752
"sprintf-js": "^1.1.1"
4853
},
4954
"scripts": {
55+
"prepare": "husky install",
5056
"test": "jest src",
5157
"lint": "eslint src",
5258
"fix": "eslint --fix src",
@@ -55,16 +61,12 @@
5561
"watch": "babel --watch src -d lib --ignore '*.test.js'",
5662
"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",
5763
"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",
58-
"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",
59-
"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"
60-
},
61-
"husky": {
62-
"hooks": {
63-
"pre-commit": "lint-staged"
64-
}
64+
"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",
65+
"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",
66+
"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'"
6567
},
6668
"lint-staged": {
67-
"src/**/*.js": "npm run lint"
69+
"src/**/*.js": "yarn lint --fix"
6870
},
6971
"bin": {
7072
"generate-api-platform-client": "./lib/index.js"

playwright.config.ts

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
import type { PlaywrightTestConfig } from '@playwright/test';
2+
import { devices } from '@playwright/test';
3+
4+
/**
5+
* Read environment variables from file.
6+
* https://github.com/motdotla/dotenv
7+
*/
8+
// require('dotenv').config();
9+
10+
/**
11+
* See https://playwright.dev/docs/test-configuration.
12+
*/
13+
const config: PlaywrightTestConfig = {
14+
testDir: './tests',
15+
/* Maximum time one test can run for. */
16+
timeout: 30 * 1000,
17+
expect: {
18+
/**
19+
* Maximum time expect() should wait for the condition to be met.
20+
* For example in `await expect(locator).toHaveText();`
21+
*/
22+
timeout: 5000
23+
},
24+
/* Run tests in files in parallel */
25+
fullyParallel: true,
26+
/* Fail the build on CI if you accidentally left test.only in the source code. */
27+
forbidOnly: !!process.env.CI,
28+
/* Retry on CI only */
29+
retries: process.env.CI ? 2 : 0,
30+
/* Opt out of parallel tests on CI. */
31+
workers: process.env.CI ? 1 : undefined,
32+
/* Reporter to use. See https://playwright.dev/docs/test-reporters */
33+
reporter: 'html',
34+
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
35+
use: {
36+
/* Maximum time each action such as `click()` can take. Defaults to 0 (no limit). */
37+
actionTimeout: 0,
38+
/* Base URL to use in actions like `await page.goto('/')`. */
39+
// baseURL: 'http://localhost:3000',
40+
41+
/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
42+
trace: 'on-first-retry',
43+
},
44+
45+
/* Configure projects for major browsers */
46+
projects: [
47+
{
48+
name: 'chromium',
49+
use: {
50+
...devices['Desktop Chrome'],
51+
},
52+
},
53+
54+
{
55+
name: 'firefox',
56+
use: {
57+
...devices['Desktop Firefox'],
58+
},
59+
},
60+
61+
{
62+
name: 'webkit',
63+
use: {
64+
...devices['Desktop Safari'],
65+
},
66+
},
67+
68+
/* Test against mobile viewports. */
69+
// {
70+
// name: 'Mobile Chrome',
71+
// use: {
72+
// ...devices['Pixel 5'],
73+
// },
74+
// },
75+
// {
76+
// name: 'Mobile Safari',
77+
// use: {
78+
// ...devices['iPhone 12'],
79+
// },
80+
// },
81+
82+
/* Test against branded browsers. */
83+
// {
84+
// name: 'Microsoft Edge',
85+
// use: {
86+
// channel: 'msedge',
87+
// },
88+
// },
89+
// {
90+
// name: 'Google Chrome',
91+
// use: {
92+
// channel: 'chrome',
93+
// },
94+
// },
95+
],
96+
97+
/* Folder for test artifacts such as screenshots, videos, traces, etc. */
98+
// outputDir: 'test-results/',
99+
100+
/* Run your local dev server before starting the tests */
101+
// webServer: {
102+
// command: 'npm run start',
103+
// port: 3000,
104+
// },
105+
};
106+
107+
export default config;

src/generators.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import fs from "fs";
2-
import NextGenerator from "./generators/NextGenerator";
3-
import NuxtGenerator from "./generators/NuxtGenerator";
4-
import ReactGenerator from "./generators/ReactGenerator";
5-
import ReactNativeGenerator from "./generators/ReactNativeGenerator";
6-
import TypescriptInterfaceGenerator from "./generators/TypescriptInterfaceGenerator";
7-
import VueGenerator from "./generators/VueGenerator";
8-
import VuetifyGenerator from "./generators/VuetifyGenerator";
9-
import QuasarGenerator from "./generators/QuasarGenerator";
2+
import NextGenerator from "./generators/NextGenerator.js";
3+
import NuxtGenerator from "./generators/NuxtGenerator.js";
4+
import ReactGenerator from "./generators/ReactGenerator.js";
5+
import ReactNativeGenerator from "./generators/ReactNativeGenerator.js";
6+
import TypescriptInterfaceGenerator from "./generators/TypescriptInterfaceGenerator.js";
7+
import VueGenerator from "./generators/VueGenerator.js";
8+
import VuetifyGenerator from "./generators/VuetifyGenerator.js";
9+
import QuasarGenerator from "./generators/QuasarGenerator.js";
1010

1111
function wrap(cl) {
1212
return ({ hydraPrefix, templateDirectory }) =>

src/generators/NextGenerator.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import chalk from "chalk";
22
import handlebars from "handlebars";
3-
import hbh_comparison from "handlebars-helpers/lib/comparison";
4-
import BaseGenerator from "./BaseGenerator";
3+
import hbh_comparison from "handlebars-helpers/lib/comparison.js";
4+
import BaseGenerator from "./BaseGenerator.js";
55

66
export default class NextGenerator extends BaseGenerator {
77
constructor(params) {
@@ -10,6 +10,7 @@ export default class NextGenerator extends BaseGenerator {
1010
this.routeAddedtoServer = false;
1111
this.registerTemplates(`next/`, [
1212
// components
13+
"components/common/Layout.tsx",
1314
"components/common/Pagination.tsx",
1415
"components/common/ReferenceLinks.tsx",
1516
"components/foo/List.tsx",
@@ -26,6 +27,7 @@ export default class NextGenerator extends BaseGenerator {
2627
"pages/foos/[id]/edit.tsx",
2728
"pages/foos/index.tsx",
2829
"pages/foos/create.tsx",
30+
"pages/_app.tsx",
2931

3032
// utils
3133
"utils/dataAccess.ts",
@@ -97,13 +99,17 @@ export default class NextGenerator extends BaseGenerator {
9799
// copy with regular name
98100
[
99101
// components
102+
"components/common/Layout.tsx",
100103
"components/common/Pagination.tsx",
101104
"components/common/ReferenceLinks.tsx",
102105

103106
// types
104107
"types/collection.ts",
105108
"types/item.ts",
106109

110+
// pages
111+
"pages/_app.tsx",
112+
107113
// utils
108114
"utils/dataAccess.ts",
109115
"utils/mercure.ts",

src/generators/NextGenerator.test.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
import { Api, Resource, Field } from "@api-platform/api-doc-parser/lib";
1+
import { Api, Resource, Field } from "@api-platform/api-doc-parser";
2+
import path from "path";
3+
import { fileURLToPath } from "url";
24
import fs from "fs";
35
import tmp from "tmp";
4-
import NextGenerator from "./NextGenerator";
6+
import NextGenerator from "./NextGenerator.js";
7+
8+
const dirname = path.dirname(fileURLToPath(import.meta.url));
59

610
const generator = new NextGenerator({
711
hydraPrefix: "hydra:",
8-
templateDirectory: `${__dirname}/../../templates`,
12+
templateDirectory: `${dirname}/../../templates`,
913
});
1014

1115
afterEach(() => {
@@ -43,6 +47,7 @@ describe("generate", () => {
4347
"/components/abc/List.tsx",
4448
"/components/abc/Show.tsx",
4549
"/components/abc/Form.tsx",
50+
"/components/common/Layout.tsx",
4651
"/components/common/ReferenceLinks.tsx",
4752
"/components/common/Pagination.tsx",
4853
"/types/Abc.ts",
@@ -52,6 +57,7 @@ describe("generate", () => {
5257
"/pages/abcs/[id]/edit.tsx",
5358
"/pages/abcs/index.tsx",
5459
"/pages/abcs/create.tsx",
60+
"/pages/_app.tsx",
5561
"/utils/dataAccess.ts",
5662
"/utils/mercure.ts",
5763
].forEach((file) => expect(fs.existsSync(tmpobj.name + file)).toBe(true));

src/generators/NuxtGenerator.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import chalk from "chalk";
2-
import BaseVueGenerator from "./VueBaseGenerator";
2+
import BaseVueGenerator from "./VueBaseGenerator.js";
33

44
export default class NuxtGenerator extends BaseVueGenerator {
55
constructor(params) {
@@ -106,9 +106,9 @@ export default class NuxtGenerator extends BaseVueGenerator {
106106

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

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

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

src/generators/NuxtGenerator.test.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
import { Api, Resource, Field } from "@api-platform/api-doc-parser/lib";
1+
import { Api, Resource, Field } from "@api-platform/api-doc-parser";
2+
import path from "path";
3+
import { fileURLToPath } from "url";
24
import fs from "fs";
35
import tmp from "tmp";
4-
import NuxtGenerator from "./NuxtGenerator";
6+
import NuxtGenerator from "./NuxtGenerator.js";
7+
8+
const dirname = path.dirname(fileURLToPath(import.meta.url));
59

610
const generator = new NuxtGenerator({
711
hydraPrefix: "hydra:",
8-
templateDirectory: `${__dirname}/../../templates`,
12+
templateDirectory: `${dirname}/../../templates`,
913
});
1014

1115
afterEach(() => {

0 commit comments

Comments
 (0)