Skip to content

Commit 9beb6bd

Browse files
authored
feat: modernize codebase (#24)
1 parent 68812ab commit 9beb6bd

File tree

14 files changed

+1062
-3720
lines changed

14 files changed

+1062
-3720
lines changed

.babelrc

Lines changed: 0 additions & 10 deletions
This file was deleted.

.eslintrc.cjs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict';
2+
3+
module.exports = {
4+
env: {
5+
es6: true,
6+
jest: true,
7+
node: true,
8+
},
9+
parser: '@typescript-eslint/parser',
10+
parserOptions: {
11+
ecmaVersion: 7,
12+
sourceType: 'module',
13+
project: ['./tsconfig.eslint.json']
14+
},
15+
plugins: ['@typescript-eslint'],
16+
extends: [
17+
'plugin:@typescript-eslint/eslint-recommended',
18+
'plugin:@typescript-eslint/recommended',
19+
'plugin:@typescript-eslint/recommended-requiring-type-checking',
20+
],
21+
};

.eslintrc.js

Lines changed: 0 additions & 17 deletions
This file was deleted.

.github/workflows/ci.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: CI
2+
3+
on:
4+
- pull_request
5+
- push
6+
7+
jobs:
8+
ci:
9+
name: Continuous integration
10+
runs-on: ubuntu-latest
11+
steps:
12+
- name: Checkout
13+
uses: actions/checkout@v2
14+
- name: Setup Node
15+
uses: actions/setup-node@v3
16+
with:
17+
node-version: 'current'
18+
- name: Install dependencies
19+
run: yarn install
20+
- name: Check build
21+
run: yarn build
22+
- name: Check coding standards
23+
run: yarn lint

.github/workflows/release.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: Release
2+
3+
on:
4+
push:
5+
tags:
6+
- 'v*'
7+
8+
jobs:
9+
release:
10+
name: Create and publish a release
11+
runs-on: ubuntu-latest
12+
steps:
13+
- name: Checkout
14+
uses: actions/checkout@v2
15+
- name: Setup Node
16+
uses: actions/setup-node@v3
17+
with:
18+
node-version: 'current'
19+
registry-url: https://registry.npmjs.org
20+
- name: Install dependencies
21+
run: yarn install
22+
- name: Check build
23+
run: yarn build
24+
- name: Check coding standards
25+
run: yarn lint
26+
- name: Publish to npm
27+
run: npm publish
28+
env:
29+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

.travis.yml

Lines changed: 0 additions & 24 deletions
This file was deleted.

README.md

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,28 @@
11
# API Platform Documentation Parsing Check
22

3-
[![Build Status](https://travis-ci.org/api-platform/api-platform-doc-parsing-check.svg?branch=master)](https://travis-ci.org/api-platform/api-platform-doc-parsing-check)
3+
[![GitHub Actions](https://github.com/api-platform/api-platform-doc-parsing-check/workflows/CI/badge.svg?branch=main)](https://github.com/api-platform/api-platform-doc-parsing-check/actions?query=workflow%3ACI+branch%3Amain)
44
[![npm version](https://badge.fury.io/js/%40api-platform%2Fapi-platform-doc-parsing-check.svg)](https://badge.fury.io/js/%40api-platform%2Fapi-platform-doc-parsing-check)
5-
[![MIT Licence](https://badges.frapsoft.com/os/mit/mit.svg?v=103)](https://opensource.org/licenses/mit-license.php)
65

76
Simple CLI app to check your API Platform's API documentation prior to run the frontend tools:
87

9-
1. [The Client Generator Component](https://api-platform.com/docs/client-generator)
10-
2. [The Admin Component](https://api-platform.com/docs/admin)
8+
1. [Create Client](https://api-platform.com/docs/create-client)
9+
2. [Admin](https://api-platform.com/docs/admin)
1110

1211
This tool is using the [API Doc Parser library](https://github.com/api-platform/api-doc-parser) that frontend tools depend on.
1312

1413
## Install
1514

16-
yarn add @api-platform/api-platform-doc-parsing-check
15+
npm install @api-platform/api-platform-doc-parsing-check
1716

1817
## Usage
1918

20-
node_modules/@api-platform/api-platform-doc-parsing-check/lib/index.js http://localhost:8080/
19+
```console
20+
# Hydra
21+
node_modules/@api-platform/api-platform-doc-parsing-check/lib/index.js http://localhost:8080/
22+
23+
# OpenAPI 3
24+
node_modules/@api-platform/api-platform-doc-parsing-check/lib/index.js -f openapi3 http://localhost:8080/docs.json
25+
```
2126

2227
Check Result:
2328

package.json

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,42 @@
11
{
22
"name": "@api-platform/api-platform-doc-parsing-check",
33
"version": "0.1.5",
4-
"description": "API-Platform Documentation parsing check",
4+
"description": "API Platform Documentation parsing check",
55
"files": [
66
"*.md",
77
"lib",
88
"src"
99
],
10-
"main": "lib/index",
10+
"bin": {
11+
"api-platform-doc-parsing-check": "./lib/index.js"
12+
},
13+
"type": "module",
14+
"exports": "./lib/index.js",
15+
"module": "./lib/index.js",
1116
"repository": "api-platform/api-platform-doc-parsing-check",
1217
"homepage": "https://github.com/api-platform/api-platform-doc-parsing-check",
1318
"bugs": "https://github.com/api-platform/api-platform-doc-parsing-check/issues",
1419
"author": "Piotr Synowiec",
1520
"license": "MIT",
1621
"devDependencies": {
17-
"babel-cli": "^6.24.0",
18-
"babel-core": "^6.24.0",
19-
"babel-eslint": "^9.0.0",
20-
"babel-jest": "^23.6.0",
21-
"babel-plugin-transform-flow-strip-types": "^6.22.0",
22-
"babel-plugin-transform-runtime": "^6.23.0",
23-
"babel-preset-es2015": "^6.24.0",
24-
"babel-preset-stage-0": "^6.22.0",
25-
"eslint": "^3.18.0",
26-
"eslint-plugin-import": "^2.2.0",
27-
"jest": "^19.0.2",
28-
"tmp": "^0.0.31"
22+
"@types/jsonld": "^1.5.0",
23+
"@types/node": "^18.0.0",
24+
"@typescript-eslint/eslint-plugin": "^5.0.0",
25+
"@typescript-eslint/parser": "^5.0.0",
26+
"eslint": "^8.0.0",
27+
"openapi-types": "^12.0.0",
28+
"typescript": "^4.1.0"
2929
},
3030
"dependencies": {
31-
"@api-platform/api-doc-parser": "^0.2",
32-
"babel-runtime": "^6.23.0",
33-
"chalk": "^2.1.0",
34-
"commander": "^2.9.0",
35-
"isomorphic-fetch": "^2.2.1"
31+
"@api-platform/api-doc-parser": "^0.15",
32+
"chalk": "^5.0.0",
33+
"commander": "^9.4.0"
3634
},
3735
"scripts": {
38-
"test": "jest",
3936
"lint": "eslint src",
40-
"build": "babel src -d lib --ignore '*.test.js'",
41-
"watch": "babel --watch src -d lib --ignore '*.test.js'",
42-
"test-parser": "npm run build && ./lib/index.js https://demo.api-platform.com && ./lib/index.js https://demo.api-platform.com -d /tmp/parsed-docs.json"
43-
},
44-
"bin": {
45-
"api-platform-doc-parsing-check": "./lib/index.js"
37+
"build": "rm -rf lib/* && tsc",
38+
"watch": "tsc --watch",
39+
"test-parser": "npm run build && chmod +x ./lib/index.js && ./lib/index.js https://demo.api-platform.com && mkdir -p tmp && ./lib/index.js https://demo.api-platform.com -d tmp/parsed-docs.json"
4640
},
4741
"publishConfig": {
4842
"access": "public"

src/index.js

Lines changed: 0 additions & 45 deletions
This file was deleted.

src/index.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/usr/bin/env node
2+
3+
import fs from 'fs';
4+
import { program } from 'commander';
5+
import chalk from 'chalk';
6+
import { parseOpenApi3Documentation, parseHydraDocumentation } from '@api-platform/api-doc-parser';
7+
import parsedJsonReplacer from "./parsedJsonReplacer.js";
8+
9+
type OptionValues = {
10+
format: string;
11+
dump: string;
12+
};
13+
14+
program
15+
.description('Check API Hydra documentation parsing')
16+
.argument('<entrypoint>', 'Entrypoint to the API documentation')
17+
.option(
18+
'-f, --format [hydra|openapi3]',
19+
'"hydra" or "openapi3"',
20+
'hydra'
21+
)
22+
.option('-d, --dump [filename]', 'Dump parsed API documentation')
23+
.showHelpAfterError()
24+
.parse();
25+
26+
const entrypoint = program.args[0];
27+
const options = program.opts<OptionValues>();
28+
29+
const parse = options.format === 'openapi3' ? parseOpenApi3Documentation : parseHydraDocumentation;
30+
31+
parse(entrypoint).then((api) => {
32+
let count = 0;
33+
const endpoints: string[] = [];
34+
console.log()
35+
console.log(`${chalk.green('Checking documentation parsing for API @')} ${entrypoint}`);
36+
for (const resource of api.api.resources || []) {
37+
count++;
38+
const nameLc = resource.name.toLowerCase();
39+
endpoints.push(nameLc);
40+
}
41+
console.log(endpoints.sort());
42+
console.log(`${chalk.green('Number of endpoints parsed:')} ${count}`);
43+
44+
if (options.dump) {
45+
fs.writeFile(options.dump, JSON.stringify(api, parsedJsonReplacer, 2), (e) => {
46+
if (e) {
47+
throw e;
48+
}
49+
console.log(`Parsed API documentation dumped to file: ${options.dump}`);
50+
});
51+
}
52+
}).catch((e) => {
53+
console.log(e);
54+
});

src/parsedJsonReplacer.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
interface ResourceValue {
2+
id?: string;
3+
title: string;
4+
}
5+
6+
type ParsedJsonReplacerResult = ResourceValue | string | null;
7+
8+
const parsedJsonReplacer = (
9+
key: string,
10+
value: null | ResourceValue
11+
): ParsedJsonReplacerResult => {
12+
if (
13+
["reference", "embedded"].includes(key) &&
14+
typeof value === "object" &&
15+
value !== null
16+
) {
17+
return `Object ${value.id || value.title}`;
18+
}
19+
20+
return value;
21+
};
22+
23+
export default parsedJsonReplacer;

tsconfig.eslint.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
"exclude": []
4+
}

tsconfig.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es6",
4+
"module": "esnext",
5+
"sourceMap": true,
6+
"outDir": "./lib",
7+
"declaration": true,
8+
"declarationMap": true,
9+
"rootDir": "./src",
10+
"importHelpers": true,
11+
"strict": true,
12+
"moduleResolution": "node",
13+
"esModuleInterop": true
14+
},
15+
"exclude": [
16+
"src/**/*.test.ts",
17+
],
18+
"include": ["./src"]
19+
}

0 commit comments

Comments
 (0)