Skip to content

Commit

Permalink
feat: modernize codebase (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
alanpoulain committed Sep 9, 2022
1 parent 68812ab commit 9beb6bd
Show file tree
Hide file tree
Showing 14 changed files with 1,062 additions and 3,720 deletions.
10 changes: 0 additions & 10 deletions .babelrc

This file was deleted.

21 changes: 21 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';

module.exports = {
env: {
es6: true,
jest: true,
node: true,
},
parser: '@typescript-eslint/parser',
parserOptions: {
ecmaVersion: 7,
sourceType: 'module',
project: ['./tsconfig.eslint.json']
},
plugins: ['@typescript-eslint'],
extends: [
'plugin:@typescript-eslint/eslint-recommended',
'plugin:@typescript-eslint/recommended',
'plugin:@typescript-eslint/recommended-requiring-type-checking',
],
};
17 changes: 0 additions & 17 deletions .eslintrc.js

This file was deleted.

23 changes: 23 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: CI

on:
- pull_request
- push

jobs:
ci:
name: Continuous integration
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Setup Node
uses: actions/setup-node@v3
with:
node-version: 'current'
- name: Install dependencies
run: yarn install
- name: Check build
run: yarn build
- name: Check coding standards
run: yarn lint
29 changes: 29 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Release

on:
push:
tags:
- 'v*'

jobs:
release:
name: Create and publish a release
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Setup Node
uses: actions/setup-node@v3
with:
node-version: 'current'
registry-url: https://registry.npmjs.org
- name: Install dependencies
run: yarn install
- name: Check build
run: yarn build
- name: Check coding standards
run: yarn lint
- name: Publish to npm
run: npm publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
24 changes: 0 additions & 24 deletions .travis.yml

This file was deleted.

17 changes: 11 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
# API Platform Documentation Parsing Check

[![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)
[![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)
[![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)
[![MIT Licence](https://badges.frapsoft.com/os/mit/mit.svg?v=103)](https://opensource.org/licenses/mit-license.php)

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

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

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

## Install

yarn add @api-platform/api-platform-doc-parsing-check
npm install @api-platform/api-platform-doc-parsing-check

## Usage

node_modules/@api-platform/api-platform-doc-parsing-check/lib/index.js http://localhost:8080/
```console
# Hydra
node_modules/@api-platform/api-platform-doc-parsing-check/lib/index.js http://localhost:8080/

# OpenAPI 3
node_modules/@api-platform/api-platform-doc-parsing-check/lib/index.js -f openapi3 http://localhost:8080/docs.json
```

Check Result:

Expand Down
46 changes: 20 additions & 26 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,48 +1,42 @@
{
"name": "@api-platform/api-platform-doc-parsing-check",
"version": "0.1.5",
"description": "API-Platform Documentation parsing check",
"description": "API Platform Documentation parsing check",
"files": [
"*.md",
"lib",
"src"
],
"main": "lib/index",
"bin": {
"api-platform-doc-parsing-check": "./lib/index.js"
},
"type": "module",
"exports": "./lib/index.js",
"module": "./lib/index.js",
"repository": "api-platform/api-platform-doc-parsing-check",
"homepage": "https://github.com/api-platform/api-platform-doc-parsing-check",
"bugs": "https://github.com/api-platform/api-platform-doc-parsing-check/issues",
"author": "Piotr Synowiec",
"license": "MIT",
"devDependencies": {
"babel-cli": "^6.24.0",
"babel-core": "^6.24.0",
"babel-eslint": "^9.0.0",
"babel-jest": "^23.6.0",
"babel-plugin-transform-flow-strip-types": "^6.22.0",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-es2015": "^6.24.0",
"babel-preset-stage-0": "^6.22.0",
"eslint": "^3.18.0",
"eslint-plugin-import": "^2.2.0",
"jest": "^19.0.2",
"tmp": "^0.0.31"
"@types/jsonld": "^1.5.0",
"@types/node": "^18.0.0",
"@typescript-eslint/eslint-plugin": "^5.0.0",
"@typescript-eslint/parser": "^5.0.0",
"eslint": "^8.0.0",
"openapi-types": "^12.0.0",
"typescript": "^4.1.0"
},
"dependencies": {
"@api-platform/api-doc-parser": "^0.2",
"babel-runtime": "^6.23.0",
"chalk": "^2.1.0",
"commander": "^2.9.0",
"isomorphic-fetch": "^2.2.1"
"@api-platform/api-doc-parser": "^0.15",
"chalk": "^5.0.0",
"commander": "^9.4.0"
},
"scripts": {
"test": "jest",
"lint": "eslint src",
"build": "babel src -d lib --ignore '*.test.js'",
"watch": "babel --watch src -d lib --ignore '*.test.js'",
"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"
},
"bin": {
"api-platform-doc-parsing-check": "./lib/index.js"
"build": "rm -rf lib/* && tsc",
"watch": "tsc --watch",
"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"
},
"publishConfig": {
"access": "public"
Expand Down
45 changes: 0 additions & 45 deletions src/index.js

This file was deleted.

54 changes: 54 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/usr/bin/env node

import fs from 'fs';
import { program } from 'commander';
import chalk from 'chalk';
import { parseOpenApi3Documentation, parseHydraDocumentation } from '@api-platform/api-doc-parser';
import parsedJsonReplacer from "./parsedJsonReplacer.js";

type OptionValues = {
format: string;
dump: string;
};

program
.description('Check API Hydra documentation parsing')
.argument('<entrypoint>', 'Entrypoint to the API documentation')
.option(
'-f, --format [hydra|openapi3]',
'"hydra" or "openapi3"',
'hydra'
)
.option('-d, --dump [filename]', 'Dump parsed API documentation')
.showHelpAfterError()
.parse();

const entrypoint = program.args[0];
const options = program.opts<OptionValues>();

const parse = options.format === 'openapi3' ? parseOpenApi3Documentation : parseHydraDocumentation;

parse(entrypoint).then((api) => {
let count = 0;
const endpoints: string[] = [];
console.log()
console.log(`${chalk.green('Checking documentation parsing for API @')} ${entrypoint}`);
for (const resource of api.api.resources || []) {
count++;
const nameLc = resource.name.toLowerCase();
endpoints.push(nameLc);
}
console.log(endpoints.sort());
console.log(`${chalk.green('Number of endpoints parsed:')} ${count}`);

if (options.dump) {
fs.writeFile(options.dump, JSON.stringify(api, parsedJsonReplacer, 2), (e) => {
if (e) {
throw e;
}
console.log(`Parsed API documentation dumped to file: ${options.dump}`);
});
}
}).catch((e) => {
console.log(e);
});
23 changes: 23 additions & 0 deletions src/parsedJsonReplacer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
interface ResourceValue {
id?: string;
title: string;
}

type ParsedJsonReplacerResult = ResourceValue | string | null;

const parsedJsonReplacer = (
key: string,
value: null | ResourceValue
): ParsedJsonReplacerResult => {
if (
["reference", "embedded"].includes(key) &&
typeof value === "object" &&
value !== null
) {
return `Object ${value.id || value.title}`;
}

return value;
};

export default parsedJsonReplacer;
4 changes: 4 additions & 0 deletions tsconfig.eslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "./tsconfig.json",
"exclude": []
}
19 changes: 19 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"compilerOptions": {
"target": "es6",
"module": "esnext",
"sourceMap": true,
"outDir": "./lib",
"declaration": true,
"declarationMap": true,
"rootDir": "./src",
"importHelpers": true,
"strict": true,
"moduleResolution": "node",
"esModuleInterop": true
},
"exclude": [
"src/**/*.test.ts",
],
"include": ["./src"]
}
Loading

0 comments on commit 9beb6bd

Please sign in to comment.