Skip to content

Commit

Permalink
fix: 🐛 fix the previous fix with a fixed json
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikHellman committed Mar 25, 2021
1 parent d795458 commit eefd791
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 19 deletions.
22 changes: 11 additions & 11 deletions lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> {
Expand All @@ -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)
Expand Down
14 changes: 6 additions & 8 deletions lib/fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,24 +46,22 @@ 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
const original = res[methodName].bind(res)
// @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
}
}
Expand Down

0 comments on commit eefd791

Please sign in to comment.