Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,46 @@ You can update
- version of api
and almost every thing from here.

### How to use multipart with SailsJS Actions2?

First, you have to add a swagger object next to the `action`.

```json
'POST /api/v1/company/upload': { action: 'company/mobile/upload', swagger: {security: [ {"ApiKey": [] } ], consumes: ["multipart/form-data"] }},
```
In the example above, we redefine the security and set the format of the passed data to `multipart/form-data`.
This is the part that's added to your `config/swagger.js`.
```json
securityDefinitions: {
"Authorization": {
"type": "apiKey",
"description": "user JWT Auth Token",
"name": "Authorization",
"in": "header",
"flow": "password"
},
"ApiKey": {
"type": "apiKey",
"description": "uses api-key",
"name":"api-key",
"in": "query",
"flow": "password"
}
}
```

When the `swagger.json` is generated there will be a list of arguments that can be used with `multipart/form-data`. To add a `file` type, you can define the following action input in controller.

```json
document: {
description: 'The file that should be uploaded.',
type: 'ref',
required: true,
swaggerType: 'file'
}
```
> Note: You have to add `files: ["document"]` to the controller at root level (next to `inputs`). For more information check [here](https://github.com/sailshq/machine-as-action#customizing-the-response)

### For Usage and more details see my [blog](https://www.logisticinfotech.com/blog/sails-hook-actions2-swagger-generator).

### TODO
Expand Down
64 changes: 46 additions & 18 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,23 +201,13 @@ module.exports = function swaggerGenerator(sails) {
pathInputs: [],
originalUrl: routeUrl
}
for (i = 0; i < urlArray.length; i++) {
if (i == 0) {
objUrl.routeUrl = urlArray[i];
} else {
objUrl.pathInputs.push(urlArray[i].split('/')[0]);
if (i == urlArray.length - 1) {
var paths = (urlArray[i]).split('/');
if (paths.length > 1) {
delete paths[0];
objUrl.routeUrl = objUrl.routeUrl + '{' + (urlArray[i]).split('/')[0] + '}' + paths.join('/');
} else {
objUrl.routeUrl = objUrl.routeUrl + '{' + (urlArray[i]).split('/')[0] + '}';
}
} else {
objUrl.routeUrl = objUrl.routeUrl + '{' + (urlArray[i]).split('/')[0] + '}' + '/';
}
}
objUrl.routeUrl = urlArray[0];
for (i = 1; i < urlArray.length; i++) {
let pathSection = urlArray[i];
let paramName = pathSection.split('/')[0];
let tail = pathSection.substring(paramName.length);
objUrl.pathInputs.push(paramName);
objUrl.routeUrl = objUrl.routeUrl + '{' + paramName + '}' + tail;
}
return objUrl;
}
Expand Down Expand Up @@ -247,7 +237,18 @@ module.exports = function swaggerGenerator(sails) {
params = generatePathData(pathInputs, 'path');
if (objUrl.methodType == "post" || objUrl.methodType == "put") {
obj = generateBodyData(objUrl.actionInputs);
params.push(obj);

if(objUrl.consumes.indexOf("multipart/form-data") > -1) {
obj = generateBodyData_MultipartForm(objUrl.actionInputs);
//append
for (let index = 0; index < obj.length; index++) {
const element = obj[index];
params.push(element);
}
} else {
//keep the old behavior
params.push(obj);
}
} else {
let tempObj = JSON.parse(JSON.stringify(generatePathData(objUrl.actionInputs, 'query')));
for (let i = 0; i < tempObj.length; i++) {
Expand Down Expand Up @@ -319,6 +320,33 @@ module.exports = function swaggerGenerator(sails) {
return obj;
}

function generateBodyData_MultipartForm(actionInputs) {
let arr = [];

for (const key in actionInputs) {
if (actionInputs.hasOwnProperty(key)) {
let inputDescriptor = actionInputs[key];
let obj = {"in": "formData", name: key};

obj.type = inputDescriptor.type === 'ref' ? 'array' : actionInputs[key].type;
if(inputDescriptor.swaggerType) {
obj.type = inputDescriptor.swaggerType
}

if (inputDescriptor.description) {
obj.description = inputDescriptor.description;
}

if (inputDescriptor.required) {
obj.required = inputDescriptor.required;
}

arr.push(obj);
}
}
return arr;
}

function generateDefinitions() {
let models = sails.models;
let defintions = {};
Expand Down