conjure-typescript
Advanced tools
Comparing version 4.1.0 to 4.2.0
@@ -18,13 +18,14 @@ /** | ||
import { IExternalReference, IListType, IMapType, IOptionalType, ISetType, IType, ITypeDefinition, ITypeName, ITypeVisitor, PrimitiveType } from "conjure-api"; | ||
export declare class MediaTypeVisitor implements ITypeVisitor<string> { | ||
import { MediaType } from "conjure-client"; | ||
export declare class MediaTypeVisitor implements ITypeVisitor<MediaType> { | ||
private knownTypes; | ||
constructor(knownTypes: Map<string, ITypeDefinition>); | ||
external: (_: IExternalReference) => string; | ||
list: (_: IListType) => string; | ||
map: (_: IMapType) => string; | ||
optional: (obj: IOptionalType) => string; | ||
primitive: (obj: PrimitiveType) => string; | ||
reference: (obj: ITypeName) => string; | ||
set: (_: ISetType) => string; | ||
unknown: (_: IType) => string; | ||
external: (_: IExternalReference) => MediaType; | ||
list: (_: IListType) => MediaType; | ||
map: (_: IMapType) => MediaType; | ||
optional: (obj: IOptionalType) => MediaType; | ||
primitive: (obj: PrimitiveType) => MediaType; | ||
reference: (obj: ITypeName) => MediaType; | ||
set: (_: ISetType) => MediaType; | ||
unknown: (_: IType) => MediaType; | ||
} |
@@ -20,2 +20,3 @@ "use strict"; | ||
var conjure_api_1 = require("conjure-api"); | ||
var conjure_client_1 = require("conjure-client"); | ||
var utils_1 = require("./utils"); | ||
@@ -27,9 +28,9 @@ var MediaTypeVisitor = /** @class */ (function () { | ||
this.external = function (_) { | ||
return "MediaType.APPLICATION_JSON"; | ||
return conjure_client_1.MediaType.APPLICATION_JSON; | ||
}; | ||
this.list = function (_) { | ||
return "MediaType.APPLICATION_JSON"; | ||
return conjure_client_1.MediaType.APPLICATION_JSON; | ||
}; | ||
this.map = function (_) { | ||
return "MediaType.APPLICATION_JSON"; | ||
return conjure_client_1.MediaType.APPLICATION_JSON; | ||
}; | ||
@@ -51,5 +52,5 @@ this.optional = function (obj) { | ||
case conjure_api_1.PrimitiveType.UUID: | ||
return "MediaType.APPLICATION_JSON"; | ||
return conjure_client_1.MediaType.APPLICATION_JSON; | ||
case conjure_api_1.PrimitiveType.BINARY: | ||
return "MediaType.APPLICATION_OCTET_STREAM"; | ||
return conjure_client_1.MediaType.APPLICATION_OCTET_STREAM; | ||
default: | ||
@@ -67,9 +68,9 @@ throw new Error("unknown primitive type"); | ||
} | ||
return "MediaType.APPLICATION_JSON"; | ||
return conjure_client_1.MediaType.APPLICATION_JSON; | ||
}; | ||
this.set = function (_) { | ||
return "MediaType.APPLICATION_JSON"; | ||
return conjure_client_1.MediaType.APPLICATION_JSON; | ||
}; | ||
this.unknown = function (_) { | ||
return "MediaType.APPLICATION_JSON"; | ||
return conjure_client_1.MediaType.APPLICATION_JSON; | ||
}; | ||
@@ -76,0 +77,0 @@ } |
@@ -20,2 +20,3 @@ "use strict"; | ||
var conjure_api_1 = require("conjure-api"); | ||
var conjure_client_1 = require("conjure-client"); | ||
var ts_simple_ast_1 = require("ts-simple-ast"); | ||
@@ -30,3 +31,2 @@ var imports_1 = require("./imports"); | ||
var HTTP_API_BRIDGE_TYPE = "IHttpApiBridge"; | ||
var MEDIA_TYPE = "MediaType"; | ||
/** Variable name used in the generation of the service class. */ | ||
@@ -38,6 +38,3 @@ var BRIDGE = "bridge"; | ||
}; | ||
var MEDIA_TYPE_IMPORT = { | ||
moduleSpecifier: utils_1.CONJURE_CLIENT, | ||
namedImports: [{ name: MEDIA_TYPE }], | ||
}; | ||
var UNDEFINED_CONSTANT = "__undefined"; | ||
function generateService(definition, knownTypes, simpleAst) { | ||
@@ -51,3 +48,8 @@ var sourceFile = simpleAst.createSourceFile(definition.serviceName); | ||
var endpointImplementations = []; | ||
var imports = [HTTP_API_BRIDGE_IMPORT, MEDIA_TYPE_IMPORT]; | ||
var imports = [HTTP_API_BRIDGE_IMPORT]; | ||
sourceFile.addVariableStatement({ | ||
declarationKind: ts_simple_ast_1.VariableDeclarationKind.Const, | ||
docs: ["Constant reference to `undefined` that we expect to get minified and therefore reduce total code size"], | ||
declarations: [{ name: UNDEFINED_CONSTANT, initializer: "undefined" }], | ||
}); | ||
definition.endpoints.forEach(function (endpointDefinition) { | ||
@@ -85,3 +87,3 @@ var parameters = endpointDefinition.args | ||
endpointImplementations.push({ | ||
bodyText: generateEndpointBody(endpointDefinition, returnTsType, mediaTypeVisitor), | ||
bodyText: generateEndpointBody(definition.serviceName.name, endpointDefinition, returnTsType, mediaTypeVisitor), | ||
name: endpointDefinition.endpointName, | ||
@@ -125,3 +127,3 @@ parameters: parameters, | ||
exports.generateService = generateService; | ||
function generateEndpointBody(endpointDefinition, returnTsType, mediaTypeVisitor) { | ||
function generateEndpointBody(serviceName, endpointDefinition, returnTsType, mediaTypeVisitor) { | ||
var bodyArgs = []; | ||
@@ -145,9 +147,9 @@ var headerArgs = []; | ||
} | ||
var data = bodyArgs.length === 0 ? "undefined" : bodyArgs[0].argName; | ||
var data = bodyArgs.length === 0 ? UNDEFINED_CONSTANT : bodyArgs[0].argName; | ||
// It's not quite correct to default to application/json for body less and return less requests. | ||
// We do this to preserve existing behaviour. | ||
var requestMediaType = bodyArgs.length === 0 ? "MediaType.APPLICATION_JSON" : conjure_api_1.IType.visit(bodyArgs[0].type, mediaTypeVisitor); | ||
var requestMediaType = bodyArgs.length === 0 ? conjure_client_1.MediaType.APPLICATION_JSON : conjure_api_1.IType.visit(bodyArgs[0].type, mediaTypeVisitor); | ||
var responseMediaType = endpointDefinition.returns !== undefined && endpointDefinition.returns !== null | ||
? conjure_api_1.IType.visit(endpointDefinition.returns, mediaTypeVisitor) | ||
: "MediaType.APPLICATION_JSON"; | ||
: conjure_client_1.MediaType.APPLICATION_JSON; | ||
var formattedHeaderArgs = headerArgs.map(function (argDefinition) { | ||
@@ -169,20 +171,35 @@ var paramId = argDefinition.paramType.header.paramId; | ||
return function (writer) { | ||
writer.write("return this." + BRIDGE + ".callEndpoint<" + returnTsType + ">(").inlineBlock(function () { | ||
writer.writeLine("binaryAsStream: true,"); | ||
writer.writeLine("data: " + data + ","); | ||
writer.writeLine("endpointName: \"" + endpointDefinition.endpointName + "\","); | ||
writer.writeLine("endpointPath: \"" + endpointDefinition.httpPath + "\","); | ||
writer.write("headers: {"); | ||
writer | ||
.write("return this." + BRIDGE + ".call<" + returnTsType + ">(") | ||
.writeLine("\"" + serviceName + "\",") | ||
.writeLine("\"" + endpointDefinition.endpointName + "\",") | ||
.writeLine("\"" + endpointDefinition.httpMethod + "\",") | ||
.writeLine("\"" + endpointDefinition.httpPath + "\",") | ||
.writeLine(data + ","); | ||
if (formattedHeaderArgs.length === 0) { | ||
writer.writeLine(UNDEFINED_CONSTANT + ","); | ||
} | ||
else { | ||
writer.write("{"); | ||
formattedHeaderArgs.forEach(function (formattedHeader) { return writer.indent().writeLine(formattedHeader); }); | ||
writer.writeLine("},"); | ||
writer.writeLine("method: \"" + endpointDefinition.httpMethod + "\","); | ||
writer.write("pathArguments: ["); | ||
} | ||
if (formattedQueryArgs.length === 0) { | ||
writer.writeLine(UNDEFINED_CONSTANT + ","); | ||
} | ||
else { | ||
writer.write("{"); | ||
formattedQueryArgs.forEach(function (formattedQuery) { return writer.indent().writeLine(formattedQuery); }); | ||
writer.writeLine("},"); | ||
} | ||
if (pathParamsFromPath.length === 0) { | ||
writer.writeLine(UNDEFINED_CONSTANT + ","); | ||
} | ||
else { | ||
writer.write("["); | ||
pathParamsFromPath.forEach(function (pathArgName) { return writer.indent().writeLine(pathArgName + ","); }); | ||
writer.writeLine("],"); | ||
writer.write("queryArguments: {"); | ||
formattedQueryArgs.forEach(function (formattedQuery) { return writer.indent().writeLine(formattedQuery); }); | ||
writer.writeLine("},"); | ||
writer.writeLine("requestMediaType: " + requestMediaType + ","); | ||
writer.writeLine("responseMediaType: " + responseMediaType + ","); | ||
}); | ||
} | ||
writer.writeLine((requestMediaType === conjure_client_1.MediaType.APPLICATION_JSON ? UNDEFINED_CONSTANT : "\"" + requestMediaType + "\"") + ","); | ||
writer.writeLine("" + (responseMediaType === conjure_client_1.MediaType.APPLICATION_JSON ? UNDEFINED_CONSTANT : "\"" + responseMediaType + "\"")); | ||
writer.write(");"); | ||
@@ -189,0 +206,0 @@ }; |
{ | ||
"name": "conjure-typescript", | ||
"version": "4.1.0", | ||
"version": "4.2.0", | ||
"description": "A conjure generator for Typescript", | ||
@@ -29,3 +29,3 @@ "bin": { | ||
"conjure-api": "^4.3.0", | ||
"conjure-client": "^2.0.0", | ||
"conjure-client": "^2.4.1", | ||
"fs-extra": "^5.0.0", | ||
@@ -32,0 +32,0 @@ "lodash": "^4.17.11", |
@@ -18,3 +18,3 @@ <p align="right"> | ||
The recommended way to use conjure-typescript is via a build tool like [gradle-conjure](https://github.com/palantir/gradle-conjure). | ||
However, if you don't want to use gradle-conjure, there is also an executable which conforms to [RFC 002](https://github.com/palantir/conjure/blob/develop/rfc/002-contract-for-conjure-generators.md), published on [bintray](https://bintray.com/palantir/releases/conjure-typescript). | ||
However, if you don't want to use gradle-conjure, there is also an executable which conforms to [RFC 002](https://github.com/palantir/conjure/blob/master/docs/rfc/002-contract-for-conjure-generators.md), published on [bintray](https://bintray.com/palantir/releases/conjure-typescript). | ||
@@ -21,0 +21,0 @@ ``` |
@@ -29,20 +29,21 @@ /** | ||
} from "conjure-api"; | ||
import { MediaType } from "conjure-client"; | ||
import { createHashableTypeName } from "./utils"; | ||
export class MediaTypeVisitor implements ITypeVisitor<string> { | ||
export class MediaTypeVisitor implements ITypeVisitor<MediaType> { | ||
constructor(private knownTypes: Map<string, ITypeDefinition>) {} | ||
public external = (_: IExternalReference): string => { | ||
return "MediaType.APPLICATION_JSON"; | ||
public external = (_: IExternalReference): MediaType => { | ||
return MediaType.APPLICATION_JSON; | ||
}; | ||
public list = (_: IListType): string => { | ||
return "MediaType.APPLICATION_JSON"; | ||
public list = (_: IListType): MediaType => { | ||
return MediaType.APPLICATION_JSON; | ||
}; | ||
public map = (_: IMapType): string => { | ||
return "MediaType.APPLICATION_JSON"; | ||
public map = (_: IMapType): MediaType => { | ||
return MediaType.APPLICATION_JSON; | ||
}; | ||
public optional = (obj: IOptionalType): string => { | ||
public optional = (obj: IOptionalType): MediaType => { | ||
return IType.visit(obj.itemType, this); | ||
}; | ||
public primitive = (obj: PrimitiveType): string => { | ||
public primitive = (obj: PrimitiveType): MediaType => { | ||
switch (obj) { | ||
@@ -59,5 +60,5 @@ case PrimitiveType.STRING: | ||
case PrimitiveType.UUID: | ||
return "MediaType.APPLICATION_JSON"; | ||
return MediaType.APPLICATION_JSON; | ||
case PrimitiveType.BINARY: | ||
return "MediaType.APPLICATION_OCTET_STREAM"; | ||
return MediaType.APPLICATION_OCTET_STREAM; | ||
default: | ||
@@ -67,3 +68,3 @@ throw new Error("unknown primitive type"); | ||
}; | ||
public reference = (obj: ITypeName): string => { | ||
public reference = (obj: ITypeName): MediaType => { | ||
const typeDefinition = this.knownTypes.get(createHashableTypeName(obj)); | ||
@@ -75,10 +76,10 @@ if (typeDefinition == null) { | ||
} | ||
return "MediaType.APPLICATION_JSON"; | ||
return MediaType.APPLICATION_JSON; | ||
}; | ||
public set = (_: ISetType): string => { | ||
return "MediaType.APPLICATION_JSON"; | ||
public set = (_: ISetType): MediaType => { | ||
return MediaType.APPLICATION_JSON; | ||
}; | ||
public unknown = (_: IType): string => { | ||
return "MediaType.APPLICATION_JSON"; | ||
public unknown = (_: IType): MediaType => { | ||
return MediaType.APPLICATION_JSON; | ||
}; | ||
} |
@@ -29,2 +29,3 @@ /** | ||
} from "conjure-api"; | ||
import { MediaType } from "conjure-client"; | ||
import { | ||
@@ -36,2 +37,3 @@ CodeBlockWriter, | ||
Scope, | ||
VariableDeclarationKind, | ||
} from "ts-simple-ast"; | ||
@@ -48,3 +50,2 @@ import { ImportsVisitor, sortImports } from "./imports"; | ||
const HTTP_API_BRIDGE_TYPE = "IHttpApiBridge"; | ||
const MEDIA_TYPE = "MediaType"; | ||
/** Variable name used in the generation of the service class. */ | ||
@@ -56,7 +57,5 @@ const BRIDGE = "bridge"; | ||
}; | ||
const MEDIA_TYPE_IMPORT = { | ||
moduleSpecifier: CONJURE_CLIENT, | ||
namedImports: [{ name: MEDIA_TYPE }], | ||
}; | ||
const UNDEFINED_CONSTANT = "__undefined"; | ||
export function generateService( | ||
@@ -75,3 +74,8 @@ definition: IServiceDefinition, | ||
const endpointImplementations: MethodDeclarationStructure[] = []; | ||
const imports: ImportDeclarationStructure[] = [HTTP_API_BRIDGE_IMPORT, MEDIA_TYPE_IMPORT]; | ||
const imports: ImportDeclarationStructure[] = [HTTP_API_BRIDGE_IMPORT]; | ||
sourceFile.addVariableStatement({ | ||
declarationKind: VariableDeclarationKind.Const, | ||
docs: ["Constant reference to `undefined` that we expect to get minified and therefore reduce total code size"], | ||
declarations: [{ name: UNDEFINED_CONSTANT, initializer: "undefined" }], | ||
}); | ||
definition.endpoints.forEach(endpointDefinition => { | ||
@@ -112,3 +116,8 @@ const parameters: ParameterDeclarationStructure[] = endpointDefinition.args | ||
endpointImplementations.push({ | ||
bodyText: generateEndpointBody(endpointDefinition, returnTsType, mediaTypeVisitor), | ||
bodyText: generateEndpointBody( | ||
definition.serviceName.name, | ||
endpointDefinition, | ||
returnTsType, | ||
mediaTypeVisitor, | ||
), | ||
name: endpointDefinition.endpointName, | ||
@@ -157,2 +166,3 @@ parameters, | ||
function generateEndpointBody( | ||
serviceName: string, | ||
endpointDefinition: IEndpointDefinition, | ||
@@ -181,11 +191,12 @@ returnTsType: string, | ||
} | ||
const data = bodyArgs.length === 0 ? "undefined" : bodyArgs[0].argName; | ||
const data = bodyArgs.length === 0 ? UNDEFINED_CONSTANT : bodyArgs[0].argName; | ||
// It's not quite correct to default to application/json for body less and return less requests. | ||
// We do this to preserve existing behaviour. | ||
const requestMediaType = | ||
bodyArgs.length === 0 ? "MediaType.APPLICATION_JSON" : IType.visit(bodyArgs[0].type, mediaTypeVisitor); | ||
bodyArgs.length === 0 ? MediaType.APPLICATION_JSON : IType.visit(bodyArgs[0].type, mediaTypeVisitor); | ||
const responseMediaType = | ||
endpointDefinition.returns !== undefined && endpointDefinition.returns !== null | ||
? IType.visit(endpointDefinition.returns, mediaTypeVisitor) | ||
: "MediaType.APPLICATION_JSON"; | ||
: MediaType.APPLICATION_JSON; | ||
const formattedHeaderArgs = headerArgs.map(argDefinition => { | ||
@@ -208,26 +219,39 @@ const paramId = (argDefinition.paramType as IParameterType_Header).header.paramId!; | ||
return writer => { | ||
writer.write(`return this.${BRIDGE}.callEndpoint<${returnTsType}>(`).inlineBlock(() => { | ||
writer.writeLine(`binaryAsStream: true,`); | ||
writer.writeLine(`data: ${data},`); | ||
writer.writeLine(`endpointName: "${endpointDefinition.endpointName}",`); | ||
writer.writeLine(`endpointPath: "${endpointDefinition.httpPath}",`); | ||
writer | ||
.write(`return this.${BRIDGE}.call<${returnTsType}>(`) | ||
.writeLine(`"${serviceName}",`) | ||
.writeLine(`"${endpointDefinition.endpointName}",`) | ||
.writeLine(`"${endpointDefinition.httpMethod}",`) | ||
.writeLine(`"${endpointDefinition.httpPath}",`) | ||
.writeLine(`${data},`); | ||
writer.write("headers: {"); | ||
if (formattedHeaderArgs.length === 0) { | ||
writer.writeLine(`${UNDEFINED_CONSTANT},`); | ||
} else { | ||
writer.write("{"); | ||
formattedHeaderArgs.forEach(formattedHeader => writer.indent().writeLine(formattedHeader)); | ||
writer.writeLine("},"); | ||
} | ||
writer.writeLine(`method: "${endpointDefinition.httpMethod}",`); | ||
if (formattedQueryArgs.length === 0) { | ||
writer.writeLine(`${UNDEFINED_CONSTANT},`); | ||
} else { | ||
writer.write("{"); | ||
formattedQueryArgs.forEach(formattedQuery => writer.indent().writeLine(formattedQuery)); | ||
writer.writeLine("},"); | ||
} | ||
writer.write("pathArguments: ["); | ||
if (pathParamsFromPath.length === 0) { | ||
writer.writeLine(`${UNDEFINED_CONSTANT},`); | ||
} else { | ||
writer.write("["); | ||
pathParamsFromPath.forEach(pathArgName => writer.indent().writeLine(pathArgName + ",")); | ||
writer.writeLine("],"); | ||
writer.write("queryArguments: {"); | ||
formattedQueryArgs.forEach(formattedQuery => writer.indent().writeLine(formattedQuery)); | ||
writer.writeLine("},"); | ||
writer.writeLine(`requestMediaType: ${requestMediaType},`); | ||
writer.writeLine(`responseMediaType: ${responseMediaType},`); | ||
}); | ||
} | ||
writer.writeLine( | ||
`${requestMediaType === MediaType.APPLICATION_JSON ? UNDEFINED_CONSTANT : `"${requestMediaType}"`},`, | ||
); | ||
writer.writeLine( | ||
`${responseMediaType === MediaType.APPLICATION_JSON ? UNDEFINED_CONSTANT : `"${responseMediaType}"`}`, | ||
); | ||
writer.write(");"); | ||
@@ -234,0 +258,0 @@ }; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
234692
4065
Updatedconjure-client@^2.4.1