Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
"use strict";
const _ = require("lodash");
const ApiGateway = require("moleculer-web");
const { UnAuthorizedError } = ApiGateway.Errors;
module.exports = {
name: "api",
mixins: [ApiGateway],
settings: {
port: process.env.PORT || 3000,
routes: [{
path: "/api",
authorization: true,
aliases: {
// Login
"POST /users/login": "users.login",
created() {
const route = _.defaultsDeep(mixinOptions.routeOptions, {
use: [
ApiGateway.serveStatic(SwaggerUI.absolutePath())
],
aliases: {
"GET /openapi.json"(req, res) {
// Send back the generated schema
if (shouldUpdateSchema || !schema) {
// Create new server & regenerate GraphQL schema
this.logger.info("♻ Regenerate OpenAPI/Swagger schema...");
try {
schema = this.generateOpenAPISchema();
shouldUpdateSchema = false;
this.logger.debug(schema);
/**
* Static routes
*/
{
path: "/",
use: [
// handle fallback for HTML5 history API
require("connect-history-api-fallback")(),
// Webpack middlewares
...initWebpackMiddlewares(),
// Serve static
ApiGateway.serveStatic("./static"),
],
// Action aliases
aliases: {
},
mappingPolicy: "restrict",
},
]
},
methods: {
/**
* Authenticate from request
*
* @param {Context} ctx
"use strict";
const { ForbiddenError } = require("moleculer-web").Errors;
const DbService = require("moleculer-db");
module.exports = {
name: "comments",
mixins: [DbService],
adapter: new DbService.MemoryAdapter({ filename: "./data/comments.db" }),
/**
* Default settings
*/
settings: {
fields: ["_id", "author", "article", "body", "createdAt", "updatedAt"],
populates: {
"author": {
action: "users.get",
params: {
"use strict";
const { MoleculerClientError } = require("moleculer").Errors;
const { ForbiddenError } = require("moleculer-web").Errors;
const _ = require("lodash");
const slug = require("slug");
const DbService = require("moleculer-db");
module.exports = {
name: "articles",
mixins: [DbService],
adapter: new DbService.MemoryAdapter({ filename: "./data/articles.db" }),
/**
* Default settings
*/
settings: {
fields: ["_id", "title", "slug", "description", "body", "tagList", "createdAt", "updatedAt", "favorited", "favoritesCount", "author", "comments"],
.then(entity => {
if (!entity)
return this.Promise.reject(new MoleculerClientError("Article not found!", 404));
if (entity.author !== ctx.meta.user._id)
return this.Promise.reject(new ForbiddenError());
return this.adapter.removeById(entity._id)
.then(() => ctx.call("favorites.removeByArticle", { article: entity._id }));
});
}
.then(comment => {
if (comment.author !== ctx.meta.user._id)
return this.Promise.reject(new ForbiddenError());
return this.adapter.removeById(ctx.params.id);
});
}