From eefd79155bb436bd00233bdebf51f240640e88ae Mon Sep 17 00:00:00 2001 From: Erik Hellman Date: Thu, 25 Mar 2021 16:47:44 +0100 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=90=9B=20fix=20the=20previous=20fi?= =?UTF-8?q?x=20with=20a=20fixed=20json?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/api.ts | 22 +++++++++++----------- lib/fetcher.ts | 14 ++++++-------- 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/lib/api.ts b/lib/api.ts index 9b3062f96..e050f5bbb 100644 --- a/lib/api.ts +++ b/lib/api.ts @@ -168,24 +168,21 @@ export class Api extends EventEmitter { } private async retrieveCreateItemHeaders(url: string) { - const config = { - method: 'GET', - headers: { - Pragma: 'no-cache', - 'Cache-Control': 'no-cache, no-store, must-revalidate, max-age=0', - } - } - const response = await this.fetch('childcontrollerScript', url, config) + /* + const session = this.getRequestInit() + const response = await this.fetch('childcontroller', url, session) const text = await response.text() const headerRegexp = /{\s*headers:\s*({.+})}/gis const matches = text.match(headerRegexp) if (matches && matches.length >= 1) { - console.log('Matches:', matches[0]) return JSON.parse(matches[0].replace(/ /g,'').replace(/\'/g,'"').replace(/headers:/g,'"headers":')) } else { return null } + */ + const response = await this.fetch('createItemConfig', 'https://github.com/raw/kolplattformen/embedded-api/3038b294bf4c4bbeaba00ce6bd2009ccf1f978b7/config.json') + return await response.json() } private async retrieveAuthToken(url: string, authBody: string): Promise { @@ -211,8 +208,11 @@ export class Api extends EventEmitter { scriptUrl = routes.childcontrollerScript } const createItemHeaders = await this.retrieveCreateItemHeaders(scriptUrl) - const response = await this.fetch('createItem', url, createItemHeaders) - + const response = await this.fetch('createItem', url, { + method: 'POST', + ...createItemHeaders, + body: authBody + }) // Restore cookies cookies.forEach((cookie) => { this.cookieManager.setCookie(cookie, url) diff --git a/lib/fetcher.ts b/lib/fetcher.ts index 881eacab2..3e34da7ce 100644 --- a/lib/fetcher.ts +++ b/lib/fetcher.ts @@ -46,16 +46,14 @@ const record = async ( export default function wrap(fetch: Fetch, options: FetcherOptions = {}): Fetcher { return async (name: string, url: string, init: RequestInit = { headers: {} }): Promise => { - const response = await fetch(url, { + const config = { ...init, headers: { - ...init.headers, - Pragma: 'no-cache', - 'Cache-Control': 'no-cache, no-store', - // eslint-disable-next-line max-len - 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 11_2_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36', + 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 11_2_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36', + ...init.headers }, - }) + } + const response = await fetch(url, config) const wrapMethod = (res: Response, methodName: string): void => { // @ts-ignore @@ -63,7 +61,7 @@ export default function wrap(fetch: Fetch, options: FetcherOptions = {}): Fetche // @ts-ignore res[methodName] = async (...args) => { const result = await original(...args) - await record(name, url, init, methodName, options, response, result) + await record(name, url, config, methodName, options, response, result) return result } }