Skip to content

Commit

Permalink
fix: fix source generator
Browse files Browse the repository at this point in the history
  • Loading branch information
pviti committed Mar 22, 2024
1 parent d4f19c6 commit 591f924
Show file tree
Hide file tree
Showing 10 changed files with 576 additions and 59 deletions.
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ gen/
src/
node_modules/
test/
specs/

tsconfig.json
tsconfig.*.json
Expand Down
7 changes: 4 additions & 3 deletions gen/fixer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* eslint-disable no-console */
import { ApiSchema } from './schema'
import { sortObjectFields } from '../src/util'
import { inspect } from 'util'



Expand Down Expand Up @@ -38,12 +37,14 @@ const fixRedundantComponents = (schema: ApiSchema): ApiSchema => {
}


const fixSchema = (schema: ApiSchema): ApiSchema => {
export const fixSchema = (schema: ApiSchema): ApiSchema => {
console.log('Fixing parsed schema...')
const fixedSchema = fixRedundantComponents(schema)
console.log('Schema fixed.')
return fixedSchema
}


export default fixSchema
export default {
fixSchema
}
8 changes: 4 additions & 4 deletions gen/generator.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@

import apiSchema, { Resource, Operation, Component, Cardinality, Attribute } from './schema'
import { readFileSync, writeFileSync, existsSync, mkdirSync, readdirSync, rmSync } from 'fs'
import { basename } from 'path'
import fixSchema from './fixer'
import { readFileSync, writeFileSync, existsSync, mkdirSync, readdirSync, rmSync } from 'node:fs'
import { basename } from 'node:path'
import Fixer from './fixer'
import Inflector from './inflector'


Expand Down Expand Up @@ -85,7 +85,7 @@ const generate = async (localSchema?: boolean) => {


// Remove redundant components and force usage of global resource component
fixSchema(schema)
Fixer.fixSchema(schema)
// console.log(inspect(schema, false, null, true))


Expand Down
12 changes: 4 additions & 8 deletions gen/schema.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import { readFileSync, writeFileSync } from 'fs'
import { snakeCase } from 'lodash'
import axios from 'axios'
import { resolve } from 'path'
import { sortObjectFields } from '../src/util'


const Inflector = require('inflector-js')
import Inflector from './inflector'


const SCHEMA_LOCAL_PATH = resolve('./gen/openapi.json')
Expand All @@ -27,8 +23,8 @@ const downloadSchema = async (url?: string): Promise<SchemaInfo> => {

console.log(`Downloading OpenAPI schema ... [${schemaUrl}]`)

const response = await axios.get(schemaUrl)
const schema = await response.data
const response = await fetch(schemaUrl)
const schema = await response.json()

if (schema) writeFileSync(schemaOutPath, JSON.stringify(schema, null, 4))
else console.log('OpenAPI schema is empty!')
Expand Down Expand Up @@ -87,7 +83,7 @@ const parseSchema = (path: string): ApiSchema => {
Object.keys(apiSchema.components).forEach(c => {
if (!specialComponentMatcher.test(c)) components[c] = apiSchema.components[c]
else
if (snakeCase(c.replace(specialComponentMatcher, '')) === singRes) resources[p].components[c] = apiSchema.components[c]
if (Inflector.snakeCase(c.replace(specialComponentMatcher, '')) === singRes) resources[p].components[c] = apiSchema.components[c]
})
// Sort components
resources[p].components = sortObjectFields(resources[p].components)
Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@
"types": "./lib/cjs/index.d.ts",
"require": "./lib/cjs/index.js",
"import": "./lib/esm/index.js"
},
"./model": {
"require": "./lib/cjs/model.js",
"import": "./lib/esm/model.js"
}
},
"sideEffects": false,
"scripts": {
"clean": "rm -rf ./.nyc_output ./node_modules/.cache ./coverage",
"build": "tsc -b tsconfig.json tsconfig.esm.json --verbose",
Expand Down
21 changes: 21 additions & 0 deletions package/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) [2023] [Commerce Layer]

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Loading

0 comments on commit 591f924

Please sign in to comment.