Skip to content
This repository has been archived by the owner on Dec 22, 2022. It is now read-only.

Commit

Permalink
feat: added blitz swagger recipe
Browse files Browse the repository at this point in the history
  • Loading branch information
prisis committed Jun 9, 2021
1 parent f788b7c commit d925770
Show file tree
Hide file tree
Showing 13 changed files with 4,825 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
root = true

[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 4
trim_trailing_whitespace = true

[*.yml]
indent_size = 2
30 changes: 30 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module.exports = {
root: true,
extends: ["@anolilab/eslint-config"],
parserOptions: {
project: "./tsconfig.eslint.json",
tsconfigRootDir: __dirname,
projectFolderIgnoreList: [".blitz/**"],
},
env: {
// Your environments (which contains several predefined global variables)
browser: true,
node: true,
commonjs: true,
es6: true,
// mocha: true,
jest: true,
// jquery: true
},
globals: {
// Your global variables (setting to false means it's not allowed to be reassigned)
//
// myGlobal: false
},
rules: {
// Customize your rules
"unicorn/no-array-for-each": "off",
"unicorn/no-null": "off",
"unicorn/no-array-reduce": "off",
},
};
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
12.20.1
Empty file added .prettierignore
Empty file.
22 changes: 22 additions & 0 deletions .prettierrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module.exports = {
"printWidth": 120,
"tabWidth": 4,
"useTabs": false,
"semi": true,
"singleQuote": false,
"quoteProps": "as-needed",
"jsxSingleQuote": false,
"trailingComma": "all",
"bracketSpacing": true,
"jsxBracketSameLine": false,
"arrowParens": "always",
"rangeStart": 0,
"rangeEnd": null,
"requirePragma": false,
"insertPragma": false,
"proseWrap": "preserve",
"htmlWhitespaceSensitivity": "css",
"vueIndentScriptAndStyle": false,
"endOfLine": "lf",
"embeddedLanguageFormatting": "auto"
}
40 changes: 40 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@

import { RecipeBuilder, paths, addImport } from "@blitzjs/installer";
import { join } from "path";
import j from "jscodeshift";
import { Collection } from "jscodeshift/src/Collection";

import pkg from "./package.json";

export default RecipeBuilder()
.setName("Tailwind UI")
.setOwner(pkg.author)
.setRepoLink(pkg.repository.url)
.addAddDependenciesStep({
stepId: "addDeps",
stepName: "Add npm dependencies",
explanation: `Swagger requires a couple of dependencies to get up and running.`,
packages: [
{ name: "swagger-jsdoc", version: "latest" },
{ name: "swagger-ui-react", version: "latest" },
{ name: "@types/swagger-jsdoc", version: "latest", isDevDep: true },
{ name: "@types/swagger-ui-react", version: "latest", isDevDep: true },
],
})
.addNewFilesStep({
stepId: "addIntegrations",
stepName: "Add Swagger helper files",
explanation: `Helper to create spec based on swagger-jsdoc`,
targetDirectory: ".",
templatePath: join(__dirname, "templates", "integrations"),
templateValues: {},
})
.addNewFilesStep({
stepId: "addSwaggerPage",
stepName: "Add Swagger page file",
explanation: `Helper to create spec based on swagger-jsdoc`,
targetDirectory: ".",
templatePath: join(__dirname, "templates", "pages"),
templateValues: {},
})
.build();
43 changes: 43 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"name": "blitzjs-swagger-recipe",
"version": "1.0.0",
"description": "",
"keywords": [
"blitzjs",
"swagger",
"react"
],
"homepage": "https://github.com/anolilab/blitzjs-swagger-recipe",
"bugs": {
"url": "https://github.com/anolilab/blitzjs-swagger-recipe/issues"
},
"repository": {
"type": "git",
"url": "https://github.com/anolilab/blitzjs-swagger-recipe.git"
},
"license": "MIT",
"author": "Daniel Bannert <[email protected]> (https://github.com/prisis)",
"main": "index.ts",
"dependencies": {
"@types/jscodeshift": "0.11.0",
"jscodeshift": "0.12.0"
},
"devDependencies": {
"@anolilab/eslint-config": "^2.1.1",
"@anolilab/prettier-config": "^2.0.1",
"@types/lodash.merge": "^4.6.6",
"@types/swagger-jsdoc": "^6.0.0",
"@types/swagger-ui-react": "^3.35.1",
"@typescript-eslint/eslint-plugin": "^4.26.1",
"@typescript-eslint/parser": "^4.26.1",
"eslint": "^7.28.0",
"eslint-plugin-jest": "^24.3.6",
"eslint-plugin-react": "^7.24.0",
"eslint-plugin-react-hooks": "^4.2.0",
"lint-staged": "^11.0.0",
"prettier": "^2.3.1",
"prettier-plugin-prisma": "^0.11.1",
"pretty-quick": "^3.1.0",
"typescript": "^4.3.2"
}
}
36 changes: 36 additions & 0 deletions templates/integrations/swagger/create-swagger-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import merge from "lodash.merge";
import path from "path";
import swaggerJsdoc from "swagger-jsdoc";

import { SwaggerOptions } from "./types";

const API_FOLDERS = [
"pages/api",
"pages/**/api",
"pages/**/api/**",
];

export default function createSwaggerSpec({
openApiVersion = "3.0.0", apiFolders = API_FOLDERS, title, version, options: swaggerOptions = {},
}: SwaggerOptions) {
const folders: string[] = []; // files containing annotations as above

apiFolders?.forEach((folder) => {
const apiDirectory = path.join(process.cwd(), folder);

folders.push(`${apiDirectory}/*.js`, `${apiDirectory}/*.ts`);
});

const options = merge({
definition: {
openapi: openApiVersion,
info: {
title,
version,
},
},
apis: folders,
}, swaggerOptions);

return swaggerJsdoc(options);
}
9 changes: 9 additions & 0 deletions templates/integrations/swagger/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import swaggerJSDoc from "swagger-jsdoc";

export type SwaggerOptions = {
openApiVersion?: string;
apiFolders?: string[];
title: string;
version: string;
options?: swaggerJSDoc.Options
};
40 changes: 40 additions & 0 deletions templates/pages/swagger/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import "swagger-ui-react/swagger-ui.css";

import { GetStaticProps, Head, InferGetStaticPropsType } from "blitz";
import createSwaggerSpec from "integrations/swagger/create-swagger-spec";
import React from "react";
import SwaggerUI from "swagger-ui-react";

import packages from "../../../package.json";

const ApiDocument = ({ spec }: InferGetStaticPropsType<typeof getStaticProps>) => (
<>
<Head>
<title>{`${packages.name} Swagger`}</title>
<style>
{`
body {
background: #fafafa !important;
}
`}
</style>
</Head>
<SwaggerUI spec={spec} />
</>
);

// eslint-disable-next-line unicorn/prevent-abbreviations
export const getStaticProps: GetStaticProps = async () => {
const spec: Record<string, any> = createSwaggerSpec({
title: `${packages.name} Swagger`,
version: packages.version,
});

return {
props: {
spec,
},
};
};

export default ApiDocument;
6 changes: 6 additions & 0 deletions tsconfig.eslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// see https://github.com/typescript-eslint/typescript-eslint/issues/890
{
"extends": "./tsconfig.json",
"exclude": ["node_modules"],
// "include": []
}
25 changes: 25 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"compilerOptions": {
"target": "ES5",
"module": "commonjs",
"checkJs": true,
"lib": ["dom", "dom.iterable", "esnext"],
"baseUrl": "./",
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"strictNullChecks": true,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"noUncheckedIndexedAccess": true,
"esModuleInterop": true,
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"incremental": true,
"tsBuildInfoFile": ".tsbuildinfo",
},
"exclude": ["node_modules", ".yarn"],
"include": ["**/*.ts", "**/*.tsx"]
}
Loading

0 comments on commit d925770

Please sign in to comment.