{"size": 1414, "ext": "js", "max_stars_repo_path": "main.js", "max_stars_repo_name": "chivincent-rosetta/insomnia-plugin-laravel-csrf", "max_stars_repo_stars_event_min_datetime": "2021-03-12T21:06:36.000Z", "max_stars_repo_stars_event_max_datetime": "2022-02-13T23:14:54.000Z", "max_issues_repo_path": "main.js", "max_issues_repo_name": "chivincent-rosetta/insomnia-plugin-laravel-csrf", "max_issues_repo_issues_event_min_datetime": "2021-04-13T14:05:02.000Z", "max_issues_repo_issues_event_max_datetime": "2021-09-18T03:31:30.000Z", "max_forks_repo_path": "main.js", "max_forks_repo_name": "chivincent-rosetta/insomnia-plugin-laravel-csrf", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 4, "max_issues_count": 2, "max_forks_count": null, "avg_line_length": 30.7391304348, "max_line_length": 97, "alphanum_fraction": 0.5919377652}
module.exports.templateTags = [{
    name: 'laravel_csrf',
    displayName: 'Laravel CSRF',
    description: 'Apply XSRF-TOKEN from cookie for X-XSRF-TOKEN request header',
    args: [
        {
            displayName: 'CSRF Cookie Name',
            type: 'string',
            defaultValue: 'XSRF-TOKEN',
            placeholder: 'It is "XSRF-TOKEN" by default in laravel',
        }
    ],
    async run(context, cookieName = 'XSRF-TOKEN') {
        const cookieJar = await this.getCookieJar(context);

        const token = cookieJar.cookies?.find(cookie => cookie.key === cookieName);

        if (token === undefined) {
            throw new Error(`${cookieName} not found in cookies`);
        }

        return decodeURIComponent(token.value);
    },

    async getCookieJar(context) {
        const { meta } = context;

        if (!meta.requestId || !meta.workspaceId) {
            throw new Error(`Request ID or workspace ID not found`);
        }

        const workspace = await context.util.models.workspace.getById(meta.workspaceId);

        if (!workspace) {
            throw new Error(`Workspace not found for ${meta.workspaceId}`);
        }

        const cookieJar = await context.util.models.cookieJar.getOrCreateForWorkspace(workspace);

        if (!cookieJar) {
            throw new Error(`Cookie jar not found for ${meta.workspaceId}`);
        }

        return cookieJar;
    },
}];