diff --git a/src/java-client.js b/src/java-client.js new file mode 100644 index 0000000..a3415af --- /dev/null +++ b/src/java-client.js @@ -0,0 +1,50 @@ +const utils = require('./utils') +const javaType = require('./java-type') + +function mapParameter(parameter, namespace) { + parameter.type = javaType.mapJavaType(parameter.type, namespace) + return parameter +} + +function mapOperation(operation, namespace) { + operation.headers = operation.parameters + .filter(param => param.location === 'Header') + .map(param => mapParameter(param, namespace)) + + operation.query = operation.parameters + .filter(param => param.location === 'Query') + .map(param => mapParameter(param, namespace)) + + operation.parameters = operation.parameters + .filter(param => param.location === 'Path') + .map(param => mapParameter(param, namespace)) + + if(operation.body) { + operation.parameters = [ + ...operation.parameters, + { + name: utils.camelcase(operation.body.type), + required: true, + location: 'Body', + type: javaType.mapJavaType(operation.body.type, namespace) + } + ] + } + + + + + return operation +} + +module.exports = { + mapResources: (resources, namespace) => { + return resources.map(resource => { + resource.type = javaType.mapJavaType(resource.type, namespace) + resource.name = utils.pascalcase(resource.plural) + resource.operations = resource.operations.map(op => mapOperation(op, namespace)) + resource.package = `${namespace}.client` + return resource + }) + } +} \ No newline at end of file diff --git a/tests/java-resource.test.js b/tests/java-resource.test.js new file mode 100644 index 0000000..e6a2f38 --- /dev/null +++ b/tests/java-resource.test.js @@ -0,0 +1,255 @@ +const util = require('util') +const javaClient = require('../src/java-client') + +const resources = [ + { + "type": "role", + "plural": "roles", + "operations": [ + { + "method": "GET", + "path": "/roles/", + "parameters": [], + "responses": [ + { + "code": { + "integer": { + "value": 200 + } + }, + "type": "[role]", + "description": "OK.", + "attributes": [] + }, + { + "code": { + "integer": { + "value": 401 + } + }, + "type": "unit", + "description": "Unauthorized.", + "attributes": [] + } + ], + "attributes": [], + "description": "Get all defined user roles." + }, + { + "method": "GET", + "path": "/roles/:id/:other", + "parameters": [ + { + "name": "id", + "type": "uuid", + "location": "Path", + "required": true + }, + { + "name": "other", + "type": "string", + "location": "Path", + "required": true + } + ], + "responses": [ + { + "code": { + "integer": { + "value": 200 + } + }, + "type": "role", + "description": "User role found.", + "attributes": [] + }, + { + "code": { + "integer": { + "value": 401 + } + }, + "type": "unit", + "description": "Unauthorized", + "attributes": [] + }, + { + "code": { + "integer": { + "value": 404 + } + }, + "type": "unit", + "description": "User role not found.", + "attributes": [] + } + ], + "attributes": [], + "description": "Get a user role for id." + }, + { + "method": "POST", + "path": "/roles/", + "parameters": [ + { + name: 'Authorization', + location: "Header", + type: "string" + } + ], + "responses": [ + { + "code": { + "integer": { + "value": 201 + } + }, + "type": "user", + "description": "User role successfully created.", + "attributes": [] + }, + { + "code": { + "integer": { + "value": 400 + } + }, + "type": "[de.nclazz.errors.v0.models.genericError]", + "description": "Bad Request.", + "attributes": [] + }, + { + "code": { + "integer": { + "value": 401 + } + }, + "type": "unit", + "description": "Unauthorized.", + "attributes": [] + } + ], + "attributes": [], + "description": "Create a new user role.", + "body": { + "type": "role_form", + "attributes": [] + } + }, + { + "method": "PUT", + "path": "/roles/:id/status", + "parameters": [ + { + "name": "id", + "type": "uuid", + "location": "Path", + "required": true + } + ], + "responses": [ + { + "code": { + "integer": { + "value": 200 + } + }, + "type": "userStatus", + "description": "User role status updated successfully.", + "attributes": [] + }, + { + "code": { + "integer": { + "value": 400 + } + }, + "type": "[de.nclazz.errors.v0.models.genericError]", + "description": "Bad Request.", + "attributes": [] + }, + { + "code": { + "integer": { + "value": 401 + } + }, + "type": "unit", + "description": "Unauthorized.", + "attributes": [] + }, + { + "code": { + "integer": { + "value": 404 + } + }, + "type": "unit", + "description": "Not Found.", + "attributes": [] + } + ], + "attributes": [], + "description": "Update the status of a user role.", + "body": { + "type": "userStatus_form", + "attributes": [] + } + }, + { + "method": "DELETE", + "path": "/roles/:id", + "parameters": [ + { + "name": "id", + "type": "uuid", + "location": "Path", + "required": true + } + ], + "responses": [ + { + "code": { + "integer": { + "value": 200 + } + }, + "type": "unit", + "description": "User role deleted successfully.", + "attributes": [] + }, + { + "code": { + "integer": { + "value": 401 + } + }, + "type": "unit", + "description": "Unauthorized.", + "attributes": [] + }, + { + "code": { + "integer": { + "value": 404 + } + }, + "type": "unit", + "description": "Not Found.", + "attributes": [] + } + ], + "attributes": [], + "description": "Mark a user role as deleted. The user role cannot be accessed by the api, but will stay in the db for a while, before completely removed." + } + ], + "attributes": [], + "path": "/roles", + "description": "Managing user roles." + }, +] + +const mapped = javaClient.mapResources(resources, "de.nclazz.test.v0") + + +console.log('Resource: ', util.inspect(mapped, { depth: 8 }), '\n-----\n') \ No newline at end of file diff --git a/tests/java-type.test.js b/tests/java-type.test.js index 06ffa54..a40a747 100644 --- a/tests/java-type.test.js +++ b/tests/java-type.test.js @@ -1,7 +1,10 @@ + const mapper = require('../src/java-type') const namespace = "de.nclazz.test.v0" + + console.log("string:", mapper.mapJavaType("string", namespace)) console.log("[string}:", mapper.mapJavaType("[string]", namespace)) console.log("custom:", mapper.mapJavaType("custom", namespace))