Skip to content

Commit

Permalink
fix: convert files from json to yaml if uploaded in yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
emilyskuo committed Jan 25, 2025
1 parent 75ae118 commit a3c5700
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/commands/openapi/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import fs from 'node:fs';
import nodePath from 'node:path';

import { Flags } from '@oclif/core';
import yaml from 'js-yaml';
import ora from 'ora';
import prompts from 'prompts';
import slugify from 'slugify';
Expand Down Expand Up @@ -101,14 +102,15 @@ export default class OpenAPIUploadCommand extends BaseCommand<typeof OpenAPIUplo
async run() {
const { spec } = this.args;

// preparedSpec is what we're uploading
const { preparedSpec, specFileType, specPath, specVersion } = await prepareOas(spec, 'openapi upload');

const version = this.flags.useSpecVersion ? specVersion : this.flags.version;

let filename = specFileType === 'url' ? nodePath.basename(specPath) : slugify.default(specPath);

const fileExtension = nodePath.extname(filename);
if (this.flags.slug) {
const fileExtension = nodePath.extname(filename);
const slugExtension = nodePath.extname(this.flags.slug);
if (slugExtension && (!['.json', '.yaml', '.yml'].includes(slugExtension) || fileExtension !== slugExtension)) {
throw new Error(
Expand Down Expand Up @@ -157,11 +159,16 @@ export default class OpenAPIUploadCommand extends BaseCommand<typeof OpenAPIUplo
this.debug('attaching URL to form data payload');
body.append('url', specPath);
} else {
// before we write prepareSpec, let's convert it back to yaml
let specToUpload = preparedSpec;
if (fileExtension === '.yaml' || fileExtension === '.yml') {
specToUpload = yaml.dump(JSON.parse(preparedSpec));
}
// Create a temporary file to write the bundled spec to,
// which we will then stream into the form data body
const { path } = await tmpFile({ prefix: 'rdme-openapi-', postfix: '.json' });
const { path } = await tmpFile({ prefix: 'rdme-openapi-', postfix: fileExtension });
this.debug(`creating temporary file at ${path}`);
fs.writeFileSync(path, preparedSpec);
fs.writeFileSync(path, specToUpload);
const stream = fs.createReadStream(path);

this.debug('file and stream created, streaming into form data payload');
Expand Down

0 comments on commit a3c5700

Please sign in to comment.