CodeSquared docs

GenerateContext

GenerateContext is an internal object controlling the generate phase. It provides access to schema data and settings inputs. It also receives generated code and writes to File objects.

Properties

PropertyTypeDescription
oasDocumentOasDocumentDocument representing the OpenAPI schema input
loggerLoggerDeno Logger for logging messages

Methods

createAndRegisterDefinition

Generates a TypeSystemValue from schema using schemaToValueFn and creates a new Definition using the provided identifier. It then registers the new definition in File object at destinationPath.

If a definition with the name provided by identifier already exists at destinationPath, the existing definition will be retrieved and returned instead of creating a new one.

context.createAndRegisterDefinition({
schema,
identifier,
destinationPath,
schemaToValueFn
})
Parameters
PropertyTypeDescription
schemaSchemaTypeInput schema which schemaToValueFn will convert to a TypeSystemValue
identifierIdentifierIdentifier representing a variable or type declaration
destinationPathstringPath to the file where the generated code will be written.
schemaToValueFn(schema: SchemaType) => TypeSystemValueFunction that takes a SchemaType object and returns a TypeSystemValue value.

Return value

A Definition object with the output of schemaToValueFn function assigned to its value property.

defineAndRegister

Creates a new Definition using the provided identifier and registers it in File object at destinationPath.

If a definition with the name provided by identifier already exists at destinationPath, the existing definition will be retrieved and returned instead of creating a new one.

context.defineAndRegister({
identifier,
value,
destinationPath,
})
Parameters
PropertyTypeDescription
identifierIdentifierIdentifier representing a variable or type declaration
valueStringableStringable value
destinationPathstringFile path for the generated code

Return value

A Definition object with the provided value assigned to its value property.

register

Insert supplied imports and definitions into File at destinationPath.

If an import from a specified module already exists in the file, the import names are appended to the existing module.

Any definitions with with a name that has already been registered at destinationPath will be not be added.

context.register({ imports, definitions })
Parameters
PropertyTypeDescription
importsRecord<string, ImportName[]> | undefinedAn optional object where each key is a module name and value is an array of ImportName objects.
definitionsDefinition[] | undefinedAn optional array of Definition objects

Return value

Void

insertOperation

Transforms an operation into a Definition using insertable and registers it.

If a Definition with same name already exists at same path, it will be reused.

If Definition is declared outside of destinationPath, it will also get imported to destinationPath.

context.insertOperation({
insertable,
operation,
generation,
destinationPath,
})
Parameters
PropertyTypeDescription
insertableOperationInsertableOperationInsertable that converts an Operation to code.
operationOasOperationObject representing the OpenAPI operation
generation'regular' | 'force' | undefinedOptional flag to force generation even if it is not selected. This option is used internally to generate dependency code.
destinationPathstringPath to the file where the generated code is used.

Return value

A new Inserted object.

insertModel

Looks up model by refName, transforms it into a Definition using insertable and registers it.

If a Definition with same name already exists at same path, it will be reused.

If Definition is declared outside of destinationPath, it will also get imported to destinationPath.

context.insertModel({
insertable,
refName,
generation,
destinationPath
})
Parameters
PropertyTypeDescription
insertableModelInsertableModelInsertable that converts an Model to code.
refNameRefNameRefName of the model to transform and insert.
generation'regular' | 'force' | undefinedOptional flag to force generation even if it is not selected. This option is used internally to generate dependency code.
destinationPathstringPath to the file where the generated code is used.

Return value

A new Inserted object.

toOperationContentSettings

Creates a new ContentSettings object from settings loaded at runtime and static config methods on insertable object.

context.toOperationContentSettings({ operation, insertable })
Parameters
PropertyTypeDescription
operationOasOperationObject representing the OpenAPI operation
insertableOperationInsertableOperationInsertable that converts an OasOperation to code.

Return value

A new ContentSettings object.

toModelContentSettings

Creates a new ContentSettings object from settings loaded at runtime and static config methods on insertable object.

context.toModelContentSettings({ refName, insertable })
Parameters
PropertyTypeDescription
refNameRefNameRefName of the model to transform and insert.
insertableModelInsertableModelInsertable that converts an Model to code.

Return value

A new ContentSettings object.

toGatewayContentSettings

Creates a new ContentSettings object from settings loaded at runtime and static config methods on gateway object.

context.toGatewayContentSettings(gateway)
Parameters
PropertyTypeDescription
gateway`OperationGatewayModelGateway`

Return value

A new ContentSettings object.

resolveSchemaRefOnce

Look up an OasSchema reference by refName and return its value.

context.resolveSchemaRefOnce( refName )
Parameters
PropertyTypeDescription
refNameRefNameRefName of the schema to resolve

Throws if schema with refName is not found.

Return value

A OasSchema or OasRef object.

findDefinition

Look up a Definition by name at exportPath.

context.findDefinition({ name, exportPath })
Parameters
PropertyTypeDescription
namestringName of the definition to find
exportPathstringPath to the file where the definition is declared

Return value

A Definition object or undefined if not found.