diff --git a/package-lock.json b/package-lock.json index 4a8ff52..6fd8952 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "xtp-typescript-bindgen", - "version": "0.0.12", + "version": "0.0.15", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "xtp-typescript-bindgen", - "version": "0.0.12", + "version": "0.0.15", "license": "BSD-3-Clause", "dependencies": { "@dylibso/xtp-bindgen": "1.0.0-rc.5", diff --git a/package.json b/package.json index 56ba100..b621c16 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xtp-typescript-bindgen", - "version": "0.0.12", + "version": "0.0.15", "description": "XTP TypeScript bindgen plugin", "main": "dist/index.js", "scripts": { diff --git a/src/index.ts b/src/index.ts index f400c94..3c65f66 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,18 +1,6 @@ import ejs from 'ejs' import { helpers, getContext, Property, Parameter } from "@dylibso/xtp-bindgen" -function needsCasting(p: Property | Parameter): boolean { - if (p.$ref) return true - - switch (p.type) { - case "string": - if (p.format === 'date-time') return true - return false - default: - return false - } -} - function toTypeScriptType(property: Property | Parameter): string { let tp if (property.$ref) { @@ -61,13 +49,18 @@ function toTypeScriptType(property: Property | Parameter): string { return `${tp} | null` } +// TODO: can move this helper up to shared library? +function isBuffer(property: Property | Parameter): boolean { + return property.type === 'buffer' +} + export function render() { const tmpl = Host.inputString() const ctx = { ...getContext(), ...helpers, + isBuffer, toTypeScriptType, - needsCasting, } const output = ejs.render(tmpl, ctx) Host.outputString(output) diff --git a/template/src/index.ts.ejs b/template/src/index.ts.ejs index cbbf878..ee39b55 100644 --- a/template/src/index.ts.ejs +++ b/template/src/index.ts.ejs @@ -11,7 +11,9 @@ import { export function <%- ex.name %>(): number { <% if (ex.input) { -%> <% if (isJsonEncoded(ex.input)) { -%> - <% if (isPrimitive(ex.input)) { -%> + <% if (isBuffer(ex.input)) { -%> + const input: <%- toTypeScriptType(ex.input) %> = Host.base64ToArrayBuffer(JSON.parse(Host.inputString())) + <% } else if (isPrimitive(ex.input)) { -%> const input: <%- toTypeScriptType(ex.input) %> = JSON.parse(Host.inputString()) <% } else { -%> const untypedInput = JSON.parse(Host.inputString()) @@ -38,7 +40,9 @@ export function <%- ex.name %>(): number { <% if (ex.output) { -%> <% if (isJsonEncoded(ex.output)) { -%> - <% if (isPrimitive(ex.output)) { -%> + <% if (isBuffer(ex.output)) { -%> + Host.outputString(JSON.stringify(Host.arrayBufferToBase64(output))) + <% } else if (isPrimitive(ex.output)) { -%> Host.outputString(JSON.stringify(output)) <% } else { -%> const untypedOutput = <%- toTypeScriptType(ex.output) %>.toJson(output) diff --git a/template/src/pdk.ts.ejs b/template/src/pdk.ts.ejs index f7cb3dd..1e0f402 100644 --- a/template/src/pdk.ts.ejs +++ b/template/src/pdk.ts.ejs @@ -24,12 +24,12 @@ export class <%- schema.name %> { return { ...obj, <% schema.properties.forEach(p => { -%> - <% if (needsCasting(p)) { -%> - <% if (isDateTime(p)) { -%> - <%- p.name -%>: obj.<%- p.name -%> ? new Date(obj.<%- p.name -%>) : null, - <% } else if (!isPrimitive(p)) {-%> - <%- p.name -%>: obj.<%- p.name -%> ? <%- p.$ref.name %>.fromJson(obj.<%- p.name %>) : null, - <% } -%> + <% if (isDateTime(p)) { -%> + <%- p.name -%>: obj.<%- p.name -%> ? new Date(obj.<%- p.name -%>) : null, + <% } else if (isBuffer(p)) {-%> + <%- p.name -%>: obj.<%- p.name -%> ? Host.base64ToArrayBuffer(obj.<%- p.name -%>) : null, + <% } else if (!isPrimitive(p)) {-%> + <%- p.name -%>: obj.<%- p.name -%> ? <%- p.$ref.name %>.fromJson(obj.<%- p.name -%>) : null, <% } -%> <% }) -%> } @@ -39,12 +39,12 @@ export class <%- schema.name %> { return { ...obj, <% schema.properties.forEach(p => { -%> - <% if (needsCasting(p)) { -%> - <% if (p.type === "string" && p.format === "date-time") { -%> - <%- p.name -%>: obj.<%- p.name -%> ? obj.<%- p.name %>.toISOString() : null, - <% } else if (p.$ref && !p.$ref.enum) {-%> - <%- p.name -%>: obj.<%- p.name -%> ? <%- p.$ref.name %>.toJson(obj.<%- p.name %>) : null, - <% } -%> + <% if (p.type === "string" && p.format === "date-time") { -%> + <%- p.name -%>: obj.<%- p.name -%> ? obj.<%- p.name %>.toISOString() : null, + <% } else if (isBuffer(p)) {-%> + <%- p.name -%>: obj.<%- p.name -%> ? Host.arrayBufferToBase64(obj.<%- p.name -%>) : null, + <% } else if (p.$ref && !p.$ref.enum) {-%> + <%- p.name -%>: obj.<%- p.name -%> ? <%- p.$ref.name %>.toJson(obj.<%- p.name -%>) : null, <% } -%> <% }) -%> } diff --git a/tests/schemas/fruit.yaml b/tests/schemas/fruit.yaml index bdf0d1f..d38c41a 100644 --- a/tests/schemas/fruit.yaml +++ b/tests/schemas/fruit.yaml @@ -1,4 +1,20 @@ exports: + topLevelBuffJSON: + description: Top level json buffers + input: + type: buffer + contentType: application/json + output: + type: buffer + contentType: application/json + topLevelBuffRaw: + description: Top level json buffers + input: + type: buffer + contentType: application/x-binary + output: + type: buffer + contentType: application/x-binary voidFunc: description: "This demonstrates how you can create an export with\nno inputs or outputs. \n"