Skip to content

Commit

Permalink
Megachange
Browse files Browse the repository at this point in the history
  • Loading branch information
davidfesteban committed Jul 26, 2024
1 parent e6fbd8b commit 0e43ee8
Show file tree
Hide file tree
Showing 13 changed files with 965 additions and 4,857 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
generated-client/
www/
apiclient.js

.idea/
node_modules/
Expand Down
151 changes: 151 additions & 0 deletions codegen.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
const yaml = require('js-yaml');
const fs = require('fs');
const path = require('path');
const schemaRoot = './openapi/schema.yaml';
const destination = './src/'

const openapiFile = fs.readFileSync(schemaRoot, 'utf8');
const openapiSchema = yaml.load(openapiFile);

const classes = generateClasses(parseModelSchema(openapiSchema));
const methods = parseMethodSchema(openapiSchema);

// Generate JavaScript classes
const jsClasses = classes.concat(methods);

// Write the generated classes to a file
fs.writeFileSync(destination.concat("apiclient.js"), jsClasses);

console.log('JavaScript classes generated successfully!');

function parseMethodSchema(fullSchema) {
let classes = `
async function fetchData(url, params, factoryFn, token) {
try {
// Construct query string from params
const queryString = new URLSearchParams(params).toString();
const fullUrl = \`\${url}?\${queryString}\`;
const response = await fetch(url, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Authorization': \`Bearer \${token}\`
}
});
if (response.status === 418) {
throw new Error(response.headers.get("Error-Message"));
}
const responseData = await response.json();
const returnedData = factoryFn(responseData);
console.log('Successful registration:', returnedData);
return returnedData;
} catch (error) {
console.error('Error:', error);
throw new Error(error.message)
}
}
async function postData(url, data, factoryFn, token) {
try {
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': \`Bearer \${token}\`
},
body: JSON.stringify(data)
});
if (response.status === 418) {
throw new Error(response.headers.get("Error-Message"));
}
const responseData = await response.json();
const returnedData = factoryFn(responseData);
console.log('Successful registration:', returnedData);
return returnedData;
} catch (error) {
console.error('Error:', error);
throw new Error(error.message)
}
}
`;
let serverUrl = fullSchema.servers[0].url;

for (const [path, methods] of Object.entries(fullSchema.paths)) {
for (const [method, details] of Object.entries(methods)) {

const methodName = details.summary.replace(/\s+/g, '');
let factoryFn = "(data) => (data)";

if(details.responses && details.responses['200'] && details.responses['200'].content) {
let className = details.responses['200'].content['application/json'].schema.$ref.split('/').pop();
factoryFn = `(data) => Object.assign(new ${className}(), data)`;
}

if (method.toUpperCase() === 'GET') {
classes += `
export async function ${methodName}(token = "", params = {}) {
return fetchData(\`${serverUrl}${path}\`, params, ${factoryFn}, token);
}\n`;
} else if (method.toUpperCase() === 'POST') {
classes += `
export async function ${methodName}(data, token = "") {
return postData(\`${serverUrl}${path}\`, data, ${factoryFn}, token);
}\n`;
}
}
}

return classes;
}

function parseModelSchema(fullSchema) {
let schemas = {};
for (const [name, schemaRef] of Object.entries(fullSchema.components.schemas)) {
if (schemaRef.$ref) {
let refSchema = loadSchema(schemaRef.$ref);
schemas[name] = refSchema;
} else {
schemas[name] = schemaRef;
}
}
return schemas;
}

function loadSchema(ref) {
const refPath = path.join(path.dirname(schemaRoot), ref);
const schemaFile = fs.readFileSync(refPath, 'utf8');
return yaml.load(schemaFile);
}

function generateClasses(schemas) {
let classes = '/** Do not edit this file. It is autogenerated **/\n\n';

for (const [className, schema] of Object.entries(schemas)) {
if (schema.type === 'object' && schema.properties) {
classes += `export class ${className} {\n`;
classes += ' constructor(data) {\n';

for (const [propName, propDef] of Object.entries(schema.properties)) {
classes += ` this.${propName} = data.${propName};\n`;
}

classes += ' }\n';
classes += '}\n\n';
}
}

return classes;
}

function generateMethods(paths) {
// Adding methods for API calls based on paths

}



8 changes: 4 additions & 4 deletions ios/App/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
PODS:
- Capacitor (6.1.0):
- Capacitor (6.1.1):
- CapacitorCordova
- CapacitorCamera (6.0.1):
- Capacitor
- CapacitorCordova (6.1.0)
- CapacitorCordova (6.1.1)
- CapacitorPreferences (6.0.1):
- Capacitor
- CapacitorSplashScreen (6.0.1):
Expand All @@ -29,9 +29,9 @@ EXTERNAL SOURCES:
:path: "../../node_modules/@capacitor/splash-screen"

SPEC CHECKSUMS:
Capacitor: 187bd7847b6f71467015a20200a1a071be3e5f14
Capacitor: 8941aba4364ba9d1b22188569001f2ce45cc2b00
CapacitorCamera: 3b77b3e8311b98220de65e63445c0e4d0b30ce3a
CapacitorCordova: be703980ca797f847c3356f78fa175d21c8330c2
CapacitorCordova: 8f2cc8d8d3619c566e9418fe8772064a94266106
CapacitorPreferences: 72909b165bc7807103778ddbb86d5d8ce06abf71
CapacitorSplashScreen: 61645214e8f955ff2b80f16a6a3648960fe4c89f

Expand Down
7 changes: 0 additions & 7 deletions openapitools.json

This file was deleted.

Loading

0 comments on commit 0e43ee8

Please sign in to comment.