fix: 🐛 fix the previous fix with a fixed json

This commit is contained in:
Erik Hellman 2021-03-25 16:47:44 +01:00
parent d7954587d5
commit eefd79155b
2 changed files with 17 additions and 19 deletions

View File

@ -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<string> {
@ -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)

View File

@ -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<Response> => {
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
}
}