Skip to content

Commit

Permalink
feat: make it possible to get schema from a component
Browse files Browse the repository at this point in the history
Does not work for reference object so far.
  • Loading branch information
Tommy Gunnarsson authored and tgun89 committed Aug 16, 2019
1 parent 82ce53c commit 0d4a558
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/bagger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import { BaggerRequestBody } from './request_body';
import { BaggerConfiguration, BaggerConfigurationInternal } from './configuration';
import { OpenAPIObject } from 'openapi3-ts';
import { BaggerParameter, ParameterType } from './parameters';
import { BaggerComponentAdder } from './component';
import { BaggerComponentAdder, BaggerComponentGetter } from './component';
import { schemaStorage } from './schema_storage';
import { JoiValidationSchema, swaggerToJoiValidation } from './utils/swagger_to_joi_validation';

const internalConfiguration = new BaggerConfigurationInternal();
const configuration = new BaggerConfiguration(internalConfiguration);
const componentAdder = new BaggerComponentAdder(internalConfiguration);
const componentGetter = new BaggerComponentGetter(internalConfiguration);

/**
* Creates a Response object
Expand Down Expand Up @@ -73,6 +74,13 @@ export function addComponent(): BaggerComponentAdder {
return componentAdder;
}

/**
* Get a reusable component.
*/
export function getComponent(): BaggerComponentGetter {
return componentGetter;
}

/**
* Create a request body used for defining a body in a bagger request
* @example
Expand Down
20 changes: 20 additions & 0 deletions lib/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ export class BaggerSchemaComponent {
}
}

public getName(): string {
return this.name;
}

public getSchema(): Schema | ReferenceObject {
return this.schema;
}

public compile(): { name: string; schema: SchemaObject | ReferenceObject } {
return {
name: this.name,
Expand All @@ -57,3 +65,15 @@ export class BaggerComponentAdder {
return component;
}
}

export class BaggerComponentGetter {
private internalConfiguration: BaggerConfigurationInternal;

public constructor(internalConfiguration: BaggerConfigurationInternal) {
this.internalConfiguration = internalConfiguration;
}

public getSchema(name: string): Schema {
return this.internalConfiguration.getSchemaComponent(name);
}
}
15 changes: 15 additions & 0 deletions lib/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import {
import { cleanObject } from './utils/clean_object';
import { validateSchema } from './utils/validate_schema';
import { BaggerSchemaComponent, SchemaComponentObject } from './component';
import { Schema } from '@hapi/joi';

class BaggerMultipleComponentsWithSameNameFoundError extends Error {}
class BaggerComponentNotFoundError extends Error {}

interface SwaggerConfiguration {
info: InfoObject;
Expand Down Expand Up @@ -87,6 +91,17 @@ export class BaggerConfigurationInternal {
this.components.schemas.push(component);
}

public getSchemaComponent(name: string): Schema {
const schemas = this.components.schemas.filter(schema => schema.getName() === name);
if (schemas.length === 0) {
throw new BaggerComponentNotFoundError();
}
if (schemas.length > 1) {
throw new BaggerMultipleComponentsWithSameNameFoundError();
}
return schemas[0].getSchema() as Schema;
}

public setInfo(info: InfoObject): void {
this.configuration.info = info;
}
Expand Down

0 comments on commit 0d4a558

Please sign in to comment.