Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Reduce boilerplate when creating crud endpoints Hooks are just middlewares that run before each of the handlers.
/pages/api/posts/[[...id]].ts
import getConfig from 'next/config'
import * as mongoose from 'mongoose'
import { createService, hook, NotFoundError } from 'api-next'
export interface PostAttrs {
title: string
}
export interface PostDoc extends mongoose.Document {
title: string
likes: number
}
interface PostModel extends mongoose.Model<PostDoc> {
build(attrs: PostAttrs): PostDoc
}
const postSchema = new mongoose.Schema({
title: {
type: String,
require: true,
},
likes: {
type: Number,
require: false,
default: 0,
},
})
const name = 'Post'
const Post = (mongoose.models[name] ||
mongoose.model<PostDoc, PostModel>(name, postSchema)) as PostModel
// From express-validator
const validateBody = hook.validateRequest(({ body, query, cookies }) => [
body('title').isString().notEmpty().withMessage('Title is required'),
])
const config = getConfig()
// Concept from Feathersjs: https://feathersjs.com/
const hooks = {
before: {
all: [
hook.connectToDatabase({
name: 'posts-db',
connect: () =>
mongoose.connect(config.serverRuntimeConfig.MONGO_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
}),
}),
],
create: [validateBody],
update: [validateBody],
},
}
// All the keys are optional
// Pick what you need
export default createService({
hooks,
find: async () => Post.find(),
create: async (body: PostAttrs) => Post.build(body),
get: async (pk) => Post.findById(pk),
update: async (pk, body: PostAttrs) => {
const post = await Post.findById(pk)
if (!post) throw new NotFoundError()
post.set(body)
await post.save()
return post
},
remove: async (pk) => {
const post = await Post.findById(pk)
if (!post) throw new NotFoundError()
await post.remove()
return { success: true, data: post }
},
})
import { createMongooseService } from 'api-next'
const { find, create, get, update, remove } = createMongooseService(Post)
export default createService({
hooks,
find,
create,
get,
update,
remove,
})
FAQs
## Usage (example with mongoose)
The npm package api-next receives a total of 12 weekly downloads. As such, api-next popularity was classified as not popular.
We found that api-next demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.