How to use the koa-swagger-decorator.body function in koa-swagger-decorator

To help you get started, we’ve selected a few koa-swagger-decorator examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github javieraviles / node-typescript-koa-rest / src / controller / user.ts View on Github external
ctx.body = 'The specified e-mail address already exists';
        } else {
            // save the user contained in the POST body
            const user = await userRepository.save(userToBeSaved);
            // return CREATED status code and updated user
            ctx.status = 201;
            ctx.body = user;
        }
    }

    @request('put', '/users/{id}')
    @summary('Update a user')
    @path({
        id: { type: 'number', required: true, description: 'id of user' }
    })
    @body(userSchema)
    public static async updateUser(ctx: BaseContext) {

        // get a user repository to perform operations with user
        const userRepository: Repository = getManager().getRepository(User);

        // update the user by specified id
        // build up entity user to be updated
        const userToBeUpdated: User = new User();
        userToBeUpdated.id = +ctx.params.id || 0; // will always have a number, this will avoid errors
        userToBeUpdated.name = ctx.request.body.name;
        userToBeUpdated.email = ctx.request.body.email;

        // validate user entity
        const errors: ValidationError[] = await validate(userToBeUpdated); // errors is an array of validation errors

        if (errors.length > 0) {
github javieraviles / node-typescript-koa-rest / src / controller / user.ts View on Github external
if (user) {
            // return OK status code and loaded user object
            ctx.status = 200;
            ctx.body = user;
        } else {
            // return a BAD REQUEST status code and error message
            ctx.status = 400;
            ctx.body = 'The user you are trying to retrieve doesn\'t exist in the db';
        }

    }

    @request('post', '/users')
    @summary('Create a user')
    @body(userSchema)
    public static async createUser(ctx: BaseContext) {

        // get a user repository to perform operations with user
        const userRepository: Repository = getManager().getRepository(User);

        // build up entity user to be saved
        const userToBeSaved: User = new User();
        userToBeSaved.name = ctx.request.body.name;
        userToBeSaved.email = ctx.request.body.email;

        // validate user entity
        const errors: ValidationError[] = await validate(userToBeSaved); // errors is an array of validation errors

        if (errors.length > 0) {
            // return BAD REQUEST status code and errors array
            ctx.status = 400;