@seamapi/blueprint
Version:
Build tools for the Seam API using this blueprint.
68 lines • 2.4 kB
JavaScript
import { camelCase, pascalCase } from 'change-case';
import { createJsonResponse } from './json.js';
export const createCsharpRequest = ({ request }, _context) => {
const parts = request.path.split('/').slice(1);
const requestArgs = formatCsharpArgs(request.parameters);
return `seam.${parts.map((p) => pascalCase(p)).join('.')}(${requestArgs})`;
};
const formatCsharpArgs = (jsonParams) => Object.entries(jsonParams)
.map(([key, value]) => {
const formattedValue = formatCsharpValue(key, value);
return `${camelCase(key)}: ${formattedValue}`;
})
.join(', ');
const formatCsharpValue = (key, value) => {
if (value == null)
return 'null';
if (typeof value === 'boolean')
return value.toString();
if (typeof value === 'number')
return value.toString();
if (typeof value === 'string')
return `"${value}"`;
if (Array.isArray(value)) {
return formatCsharpArray(key, value);
}
if (typeof value === 'object') {
return formatCsharpObject(value);
}
throw new Error(`Unsupported type: ${typeof value}`);
};
const formatCsharpArray = (key, value) => {
if (value.length === 0) {
return 'new object[] { }';
}
const formattedItems = value.map((v) => formatCsharpValue(key, v));
const item = value[0];
if (item == null) {
throw new Error(`Null value in response array for '${key}'`);
}
const arrayType = isPrimitiveValue(item)
? getPrimitiveTypeName(item)
: 'object';
return `new ${arrayType}[] { ${formattedItems.join(', ')}} `;
};
const isPrimitiveValue = (value) => value !== null && typeof value !== 'object';
const getPrimitiveTypeName = (value) => {
switch (typeof value) {
case 'string':
return 'string';
case 'number':
return 'float';
case 'boolean':
return 'bool';
default:
throw new Error(`Unsupported type: ${typeof value}`);
}
};
const formatCsharpObject = (value) => {
if (Object.keys(value).length === 0) {
return 'new { }';
}
const formattedEntries = Object.entries(value)
.map(([objKey, val]) => `${camelCase(objKey)} = ${formatCsharpValue(objKey, val)}`)
.join(', ');
return `new { ${formattedEntries} }`;
};
export const createCsharpResponse = createJsonResponse;
//# sourceMappingURL=csharp.js.map