-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add api to create flags #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sahsisunny
wants to merge
1
commit into
get-projects-api
Choose a base branch
from
create-flags-api
base: get-projects-api
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| import Joi from 'joi'; | ||
| import { NextFunction, Request, Response } from 'express'; | ||
|
|
||
| const errorMessages = { | ||
| name: { | ||
| 'string.empty': 'Flag name cannot be empty', | ||
| 'any.required': 'Flag name is required', | ||
| }, | ||
| description: { | ||
| 'string.empty': 'Description cannot be empty if provided', | ||
| }, | ||
| key: { | ||
| 'string.empty': 'Flag key cannot be empty', | ||
| 'any.required': 'Flag key is required', | ||
| }, | ||
| project_id: { | ||
| 'string.empty': 'Project ID cannot be empty', | ||
| 'any.required': 'Project ID is required', | ||
| }, | ||
| environment_id: { | ||
| 'string.empty': 'Environment ID cannot be empty', | ||
| 'any.required': 'Environment ID is required', | ||
| }, | ||
| is_active: { | ||
| 'boolean.base': 'Is active must be a boolean value', | ||
| 'any.required': 'Is active status is required', | ||
| }, | ||
| expires_at: { | ||
| 'date.base': 'Expiration date must be a valid date', | ||
| 'date.greater': 'Expiration date must be in the future', | ||
| }, | ||
| added_by: { | ||
| 'string.empty': 'Added by cannot be empty', | ||
| 'any.required': 'Added by is required', | ||
| }, | ||
| }; | ||
|
|
||
| export const createFlagValidator = Joi.object({ | ||
| name: Joi.string() | ||
| .trim() | ||
| .min(3) | ||
| .max(100) | ||
| .required() | ||
| .messages(errorMessages.name), | ||
|
|
||
| description: Joi.string() | ||
| .trim() | ||
| .max(500) | ||
| .allow('') | ||
| .optional() | ||
| .messages(errorMessages.description), | ||
|
|
||
| key: Joi.string() | ||
| .trim() | ||
| .pattern(/^[a-zA-Z0-9_-]+$/) | ||
| .min(3) | ||
| .max(50) | ||
| .required() | ||
| .messages({ | ||
| ...errorMessages.key, | ||
| 'string.pattern.base': | ||
| 'Flag key must contain only letters, numbers, hyphens and underscores', | ||
| }), | ||
|
|
||
| project_id: Joi.string() | ||
| .trim() | ||
| .uuid() | ||
| .required() | ||
| .messages({ | ||
| ...errorMessages.project_id, | ||
| 'string.guid': 'Project ID must be a valid UUID', | ||
| }), | ||
|
|
||
| environment_id: Joi.string() | ||
| .trim() | ||
| .uuid() | ||
| .required() | ||
| .messages({ | ||
| ...errorMessages.environment_id, | ||
| 'string.guid': 'Environment ID must be a valid UUID', | ||
| }), | ||
|
|
||
| is_active: Joi.boolean().required().messages(errorMessages.is_active), | ||
|
|
||
| expires_at: Joi.date() | ||
| .greater('now') | ||
| .allow(null) | ||
| .optional() | ||
| .messages({ | ||
| ...errorMessages.expires_at, | ||
| 'date.greater': 'Expiration date must be in the future', | ||
| }), | ||
|
|
||
| added_by: Joi.string() | ||
| .trim() | ||
| .uuid() | ||
| .required() | ||
| .messages({ | ||
| ...errorMessages.added_by, | ||
| 'string.guid': 'Added by must be a valid UUID', | ||
| }), | ||
| }); | ||
|
|
||
| export const validateCreateFlag = async ( | ||
| req: Request, | ||
| res: Response, | ||
| next: NextFunction, | ||
| ) => { | ||
| try { | ||
| await createFlagValidator.validateAsync(req.body); | ||
| next(); | ||
| } catch (error: unknown) { | ||
| if (error instanceof Error) { | ||
| res.status(400).json({ error: error.message }); | ||
| } else { | ||
| res.status(400).json({ error: 'An unknown error occurred' }); | ||
| } | ||
| } | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,8 @@ | ||
| import { getFlagsController } from '../controllers/flags'; | ||
| import { getFlagsController, createFlagController } from '../controllers/flags'; | ||
| import express from 'express'; | ||
| import { validateCreateFlag } from '../middlewares/flags'; | ||
|
|
||
| export default (router: express.Router) => { | ||
| router.get('/flags', getFlagsController); | ||
| router.post('/flags', validateCreateFlag, createFlagController); | ||
| }; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.