Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
simcaq-node
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
simcaq
simcaq-node
Commits
8de89944
There was a problem fetching the pipeline summary.
Commit
8de89944
authored
6 years ago
by
Vytor Calixto
Browse files
Options
Downloads
Patches
Plain Diff
Add function in response middleware 2 flat objects
parent
7ba4aac4
No related branches found
No related tags found
No related merge requests found
Pipeline
#
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/libs/middlewares/response.js
+41
-3
41 additions, 3 deletions
src/libs/middlewares/response.js
with
41 additions
and
3 deletions
src/libs/middlewares/response.js
+
41
−
3
View file @
8de89944
...
...
@@ -3,13 +3,51 @@ const log = require(`${libs}/log`)(module);
const
xml
=
require
(
'
js2xmlparser
'
);
const
csv
=
require
(
'
csv-express
'
);
// Custom generic middleware used respond requests.
// The function reads the req.query.format param and respond in json, xml or csv
// Função para transformar um resultado que contém objetos com arrays aninhados em vários objetos
// sem arrays aninhandos
function
flatObj
(
obj
)
{
let
flatList
=
[];
let
tempObj
=
{};
Object
.
keys
(
obj
).
forEach
((
key
)
=>
{
if
(
obj
[
key
]
instanceof
Array
)
{
obj
[
key
].
forEach
((
i
)
=>
{
let
flatten
=
flatObj
(
i
);
if
(
flatten
instanceof
Array
)
{
flatten
.
forEach
((
j
)
=>
{
flatList
.
push
(
Object
.
assign
({},
tempObj
,
j
));
});
}
else
{
flatList
.
push
(
Object
.
assign
({},
tempObj
,
flatten
));
}
});
}
else
if
(
obj
[
key
]
instanceof
Object
)
{
tempObj
=
Object
.
assign
({},
tempObj
,
obj
[
key
]);
}
else
{
tempObj
[
key
]
=
obj
[
key
];
}
});
if
(
flatList
.
length
>
0
)
return
flatList
;
return
tempObj
;
}
// Custom generic middleware used to answer requests.
// The function reads the req.query.format param and answers in json, xml or csv
function
response
(
value
)
{
return
(
req
,
res
,
next
)
=>
{
if
(
req
.
query
.
format
===
'
csv
'
)
{
let
result
=
[];
req
.
result
.
forEach
((
i
)
=>
{
let
flatten
=
flatObj
(
i
);
if
(
flatten
instanceof
Array
)
{
result
=
[...
result
,
...
flatten
];
}
else
{
result
.
push
(
flatten
);
}
});
res
.
attachment
(
`
${
value
}
.csv`
);
res
.
csv
(
req
.
result
,
true
);
res
.
csv
(
result
,
true
);
}
else
if
(
req
.
query
.
format
===
'
xml
'
)
{
res
.
send
(
xml
.
parse
(
'
result
'
,
{
[
value
]:
req
.
result
}));
}
else
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment