mirror of
https://github.com/TandoorRecipes/recipes.git
synced 2026-01-08 23:58:15 -05:00
central cookie function
This commit is contained in:
@@ -1,5 +1,7 @@
|
|||||||
/* tslint:disable */
|
/* tslint:disable */
|
||||||
/* eslint-disable */
|
/* eslint-disable */
|
||||||
|
import {getCookie} from "@/utils/cookie";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Django Recipes
|
* Django Recipes
|
||||||
* No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
|
* No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
|
||||||
@@ -140,21 +142,6 @@ export class BaseAPI {
|
|||||||
throw new ResponseError(response, 'Response returned an error code');
|
throw new ResponseError(response, 'Response returned an error code');
|
||||||
}
|
}
|
||||||
|
|
||||||
private getCookie(name: string) {
|
|
||||||
let cookieValue = null;
|
|
||||||
if (document.cookie && document.cookie !== '') {
|
|
||||||
const cookies = document.cookie.split(';');
|
|
||||||
for (let i = 0; i < cookies.length; i++) {
|
|
||||||
const cookie = cookies[i].trim();
|
|
||||||
// Does this cookie string begin with the name we want?
|
|
||||||
if (cookie.substring(0, name.length + 1) === (name + '=')) {
|
|
||||||
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return cookieValue;
|
|
||||||
}
|
|
||||||
private async createFetchParams(context: RequestOpts, initOverrides?: RequestInit | InitOverrideFunction) {
|
private async createFetchParams(context: RequestOpts, initOverrides?: RequestInit | InitOverrideFunction) {
|
||||||
let url = this.configuration.basePath + context.path;
|
let url = this.configuration.basePath + context.path;
|
||||||
if (context.query !== undefined && Object.keys(context.query).length !== 0) {
|
if (context.query !== undefined && Object.keys(context.query).length !== 0) {
|
||||||
@@ -164,7 +151,7 @@ export class BaseAPI {
|
|||||||
url += '?' + this.configuration.queryParamsStringify(context.query);
|
url += '?' + this.configuration.queryParamsStringify(context.query);
|
||||||
}
|
}
|
||||||
|
|
||||||
const headers = Object.assign({}, this.configuration.headers, context.headers, { 'X-CSRFToken': this.getCookie('csrftoken'), });
|
const headers = Object.assign({}, this.configuration.headers, context.headers, { 'X-CSRFToken': getCookie('csrftoken'), });
|
||||||
Object.keys(headers).forEach(key => headers[key] === undefined ? delete headers[key] : {});
|
Object.keys(headers).forEach(key => headers[key] === undefined ? delete headers[key] : {});
|
||||||
|
|
||||||
const initOverrideFn =
|
const initOverrideFn =
|
||||||
|
|||||||
19
vue3/src/utils/cookie.ts
Normal file
19
vue3/src/utils/cookie.ts
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
/**
|
||||||
|
* simple function to retrieve a cookie by name
|
||||||
|
* @param name name of the cookie
|
||||||
|
*/
|
||||||
|
export function getCookie(name: string) {
|
||||||
|
let cookieValue = null;
|
||||||
|
if (document.cookie && document.cookie !== '') {
|
||||||
|
const cookies = document.cookie.split(';');
|
||||||
|
for (let i = 0; i < cookies.length; i++) {
|
||||||
|
const cookie = cookies[i].trim();
|
||||||
|
// Does this cookie string begin with the name we want?
|
||||||
|
if (cookie.substring(0, name.length + 1) === (name + '=')) {
|
||||||
|
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return cookieValue;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user