diff --git a/lib/api.ts b/lib/api.ts index 9b3062f9..e050f5bb 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://raw.githubusercontent.com/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 881eacab..3e34da7c 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 } }