How to use the @webiny/api.Response function in @webiny/api

To help you get started, we’ve selected a few @webiny/api 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 webiny / webiny-js / packages / api-forms / src / plugins / graphql / formSubmissionResolvers / exportFormSubmissions.js View on Github external
rows.push(row);
    }

    // Save CSV file and return its URL to the client.
    const csv = await parseAsync(rows, { fields: Object.values(fields) });
    const buffer = Buffer.from(csv);
    const result = await uploadFile({
        context,
        buffer,
        file: {
            size: buffer.length,
            name: "form_submissions_export.csv",
            type: "text/csv"
        }
    });
    return new Response(result);
};
github webiny / webiny-js / packages / api-page-builder / src / plugins / graphql / pageResolvers / getPublishedPage.js View on Github external
export default async (root: any, args: Object, context: Object) => {
    if (!args.parent && !args.url) {
        return new NotFoundResponse("Page parent or URL missing.");
    }

    // We utilize the same query used for listing published pages (single source of truth = less maintenance).
    const [page] = await listPublishedPages({ context, args: { ...args, perPage: 1 } });

    if (!page) {
        return new NotFoundResponse("The requested page was not found.");
    }

    return new Response(page);
};
github webiny / webiny-js / packages / api-page-builder / src / plugins / graphql / installResolver / install.js View on Github external
export const isInstalled = async (root: any, args: Object, context: Object) => {
    const { PbSettings } = context.models;
    const settings = await PbSettings.load();
    if (!settings) {
        return new Response(false);
    }

    return new Response(settings.data.installation.completed);
};
github webiny / webiny-js / packages / api-security / src / plugins / graphql / userResolvers / createUser.js View on Github external
} catch (e) {
        if (e.code === WithFieldsError.VALIDATION_FAILED_INVALID_FIELDS) {
            const attrError = InvalidFieldsError.from(e);
            return new ErrorResponse({
                code: attrError.code || WithFieldsError.VALIDATION_FAILED_INVALID_FIELDS,
                message: attrError.message,
                data: attrError.data
            });
        }
        return new ErrorResponse({
            code: e.code,
            message: e.message,
            data: e.data
        });
    }
    return new Response(user);
};
github webiny / webiny-js / packages / api-forms / src / plugins / graphql / formResolvers / getPublishedForm.js View on Github external
const { forms, totalCount } = await plugin.resolve({ root, args: listArgs, context });

    if (!Array.isArray(forms) || !Number.isInteger(totalCount)) {
        throw Error(
            `Resolver plugin "forms-resolver-list-published-forms" must return { forms: [Form], totalCount: Int }!`
        );
    }

    const [form] = forms;

    if (!form) {
        return new NotFoundResponse("The requested form was not found.");
    }

    return new Response(form);
};
github webiny / webiny-js / packages / api-files / src / plugins / resolvers / createFiles.js View on Github external
const { File } = context.models;

    const data = [];
    for (let i = 0; i < files.length; i++) {
        let item = files[i];
        let file = await File.findById(item.id);
        if (!file) {
            file = new File();
        }

        await file.populate(item).save();
        data.push(file);
    }

    return new Response(data);
};
github webiny / webiny-js / packages / commodo-graphql / src / crudResolvers.js View on Github external
e.code === WithFieldsError.VALIDATION_FAILED_INVALID_FIELDS
        ) {
            const fieldError = InvalidFieldsError.from(e);
            return new ErrorResponse({
                code: fieldError.code || WithFieldsError.VALIDATION_FAILED_INVALID_FIELDS,
                message: fieldError.message,
                data: fieldError.data
            });
        }
        return new ErrorResponse({
            code: e.code,
            message: e.message,
            data: e.data || null
        });
    }
    return new Response(model);
};
github webiny / webiny-js / packages / api-page-builder / src / plugins / graphql / installResolver / install.js View on Github external
export const isInstalled = async (root: any, args: Object, context: Object) => {
    const { PbSettings } = context.models;
    const settings = await PbSettings.load();
    if (!settings) {
        return new Response(false);
    }

    return new Response(settings.data.installation.completed);
};
github webiny / webiny-js / packages / api-security / src / plugins / graphql / installResolvers / install.js View on Github external
if (e.code === WithFieldsError.VALIDATION_FAILED_INVALID_FIELDS) {
            const attrError = InvalidFieldsError.from(e);
            return new ErrorResponse({
                code: attrError.code || WithFieldsError.VALIDATION_FAILED_INVALID_FIELDS,
                message: attrError.message,
                data: attrError.data
            });
        }
        return new ErrorResponse({
            code: e.code,
            message: e.message,
            data: e.data
        });
    }

    return new Response(result);
};
github webiny / webiny-js / packages / api-security / src / plugins / graphql / userResolvers / deleteUser.js View on Github external
if (!user) {
        return new NotFoundResponse(id ? `User "${id}" not found!` : "User not found!");
    }

    try {
        await user.delete();

        const authPlugin = context.plugins
            .byType("security-authentication-provider")
            .filter(pl => pl.hasOwnProperty("deleteUser"))
            .pop();

        await authPlugin.deleteUser({ user }, context);

        return new Response(true);
    } catch (e) {
        return new ErrorResponse({
            code: e.code,
            message: e.message,
            data: e.data
        });
    }
};