feat: native interceptor can work with cookies
This commit is contained in:
12
packages/hoppscotch-common/src/components.d.ts
vendored
12
packages/hoppscotch-common/src/components.d.ts
vendored
@@ -1,11 +1,11 @@
|
|||||||
/* eslint-disable */
|
// generated by unplugin-vue-components
|
||||||
/* prettier-ignore */
|
// We suggest you to commit this file into source control
|
||||||
// @ts-nocheck
|
|
||||||
// Generated by unplugin-vue-components
|
|
||||||
// Read more: https://github.com/vuejs/core/pull/3399
|
// Read more: https://github.com/vuejs/core/pull/3399
|
||||||
|
import '@vue/runtime-core'
|
||||||
|
|
||||||
export {}
|
export {}
|
||||||
|
|
||||||
declare module 'vue' {
|
declare module '@vue/runtime-core' {
|
||||||
export interface GlobalComponents {
|
export interface GlobalComponents {
|
||||||
AppActionHandler: typeof import('./components/app/ActionHandler.vue')['default']
|
AppActionHandler: typeof import('./components/app/ActionHandler.vue')['default']
|
||||||
AppAnnouncement: typeof import('./components/app/Announcement.vue')['default']
|
AppAnnouncement: typeof import('./components/app/Announcement.vue')['default']
|
||||||
@@ -59,7 +59,6 @@ declare module 'vue' {
|
|||||||
CollectionsSaveRequest: typeof import('./components/collections/SaveRequest.vue')['default']
|
CollectionsSaveRequest: typeof import('./components/collections/SaveRequest.vue')['default']
|
||||||
CollectionsTeamCollections: typeof import('./components/collections/TeamCollections.vue')['default']
|
CollectionsTeamCollections: typeof import('./components/collections/TeamCollections.vue')['default']
|
||||||
CookiesAllModal: typeof import('./components/cookies/AllModal.vue')['default']
|
CookiesAllModal: typeof import('./components/cookies/AllModal.vue')['default']
|
||||||
CookiesCookieJarModal: typeof import('./components/cookies/CookieJarModal.vue')['default']
|
|
||||||
CookiesEditCookie: typeof import('./components/cookies/EditCookie.vue')['default']
|
CookiesEditCookie: typeof import('./components/cookies/EditCookie.vue')['default']
|
||||||
Environments: typeof import('./components/environments/index.vue')['default']
|
Environments: typeof import('./components/environments/index.vue')['default']
|
||||||
EnvironmentsAdd: typeof import('./components/environments/Add.vue')['default']
|
EnvironmentsAdd: typeof import('./components/environments/Add.vue')['default']
|
||||||
@@ -219,4 +218,5 @@ declare module 'vue' {
|
|||||||
WorkspaceCurrent: typeof import('./components/workspace/Current.vue')['default']
|
WorkspaceCurrent: typeof import('./components/workspace/Current.vue')['default']
|
||||||
WorkspaceSelector: typeof import('./components/workspace/Selector.vue')['default']
|
WorkspaceSelector: typeof import('./components/workspace/Selector.vue')['default']
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,6 +10,7 @@
|
|||||||
"tauri": "tauri"
|
"tauri": "tauri"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"dioc": "workspace:^",
|
||||||
"@hoppscotch/common": "workspace:^",
|
"@hoppscotch/common": "workspace:^",
|
||||||
"@platform/auth": "^0.1.106",
|
"@platform/auth": "^0.1.106",
|
||||||
"@tauri-apps/api": "^1.3.0",
|
"@tauri-apps/api": "^1.3.0",
|
||||||
|
|||||||
@@ -5,9 +5,9 @@ import { def as collectionsDef } from "./platform/collections/collections.platfo
|
|||||||
import { def as settingsDef } from "./platform/settings/settings.platform"
|
import { def as settingsDef } from "./platform/settings/settings.platform"
|
||||||
import { def as historyDef } from "./platform/history/history.platform"
|
import { def as historyDef } from "./platform/history/history.platform"
|
||||||
import { def as tabStateDef } from "./platform/tabState/tabState.platform"
|
import { def as tabStateDef } from "./platform/tabState/tabState.platform"
|
||||||
import { nativeInterceptor } from "./platform/interceptors/native"
|
|
||||||
import { proxyInterceptor } from "@hoppscotch/common/platform/std/interceptors/proxy"
|
import { proxyInterceptor } from "@hoppscotch/common/platform/std/interceptors/proxy"
|
||||||
import { ExtensionInspectorService } from "@hoppscotch/common/platform/std/inspections/extension.inspector"
|
import { ExtensionInspectorService } from "@hoppscotch/common/platform/std/inspections/extension.inspector"
|
||||||
|
import { NativeInterceptorService } from "./platform/interceptors/native"
|
||||||
import { nextTick, ref, watch } from "vue"
|
import { nextTick, ref, watch } from "vue"
|
||||||
import { emit, listen } from "@tauri-apps/api/event"
|
import { emit, listen } from "@tauri-apps/api/event"
|
||||||
import { type } from "@tauri-apps/api/os"
|
import { type } from "@tauri-apps/api/os"
|
||||||
@@ -40,7 +40,7 @@ createHoppApp("#app", {
|
|||||||
interceptors: {
|
interceptors: {
|
||||||
default: "native",
|
default: "native",
|
||||||
interceptors: [
|
interceptors: [
|
||||||
{ type: "standalone", interceptor: nativeInterceptor },
|
{ type: "service", service: NativeInterceptorService },
|
||||||
{ type: "standalone", interceptor: proxyInterceptor },
|
{ type: "standalone", interceptor: proxyInterceptor },
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -4,9 +4,11 @@ import {
|
|||||||
InterceptorError,
|
InterceptorError,
|
||||||
RequestRunResult,
|
RequestRunResult,
|
||||||
} from "@hoppscotch/common/services/interceptor.service"
|
} from "@hoppscotch/common/services/interceptor.service"
|
||||||
|
import { CookieJarService } from "@hoppscotch/common/services/cookie-jar.service"
|
||||||
import axios, { AxiosRequestConfig, CancelToken } from "axios"
|
import axios, { AxiosRequestConfig, CancelToken } from "axios"
|
||||||
import { cloneDeep } from "lodash-es"
|
import { cloneDeep } from "lodash-es"
|
||||||
import { Body, HttpVerb, ResponseType, getClient } from '@tauri-apps/api/http'
|
import { Body, HttpVerb, ResponseType, getClient } from "@tauri-apps/api/http"
|
||||||
|
import { Service } from "dioc"
|
||||||
|
|
||||||
export const preProcessRequest = (
|
export const preProcessRequest = (
|
||||||
req: AxiosRequestConfig
|
req: AxiosRequestConfig
|
||||||
@@ -55,19 +57,19 @@ async function runRequest(
|
|||||||
if (processedReq.data instanceof FormData) {
|
if (processedReq.data instanceof FormData) {
|
||||||
let body_data = {}
|
let body_data = {}
|
||||||
for (const entry of processedReq.data.entries()) {
|
for (const entry of processedReq.data.entries()) {
|
||||||
const [name, value] = entry;
|
const [name, value] = entry
|
||||||
|
|
||||||
if (value instanceof File) {
|
if (value instanceof File) {
|
||||||
let file_data = await value.arrayBuffer()
|
let file_data = await value.arrayBuffer()
|
||||||
|
|
||||||
body_data[name] = {
|
body_data[name] = {
|
||||||
file: new Uint8Array(file_data),
|
file: new Uint8Array(file_data),
|
||||||
fileName: value.name
|
fileName: value.name,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
body = Body.form(body_data);
|
body = Body.form(body_data)
|
||||||
}
|
}
|
||||||
|
|
||||||
const res = await client.request({
|
const res = await client.request({
|
||||||
@@ -75,15 +77,15 @@ async function runRequest(
|
|||||||
url: processedReq.url ?? "",
|
url: processedReq.url ?? "",
|
||||||
responseType: ResponseType.Binary,
|
responseType: ResponseType.Binary,
|
||||||
headers: processedReq.headers,
|
headers: processedReq.headers,
|
||||||
body: body
|
body: body,
|
||||||
});
|
})
|
||||||
|
|
||||||
if (cancelled()) {
|
if (cancelled()) {
|
||||||
client.drop()
|
client.drop()
|
||||||
return E.left("cancellation")
|
return E.left("cancellation")
|
||||||
}
|
}
|
||||||
|
|
||||||
res.data = new Uint8Array(res.data as number[]).buffer;
|
res.data = new Uint8Array(res.data as number[]).buffer
|
||||||
|
|
||||||
const timeEnd = Date.now()
|
const timeEnd = Date.now()
|
||||||
|
|
||||||
@@ -123,20 +125,45 @@ async function runRequest(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const nativeInterceptor: Interceptor = {
|
export class NativeInterceptorService extends Service implements Interceptor {
|
||||||
interceptorID: "native",
|
public static readonly ID = "NATIVE_INTERCEPTOR_SERVICE"
|
||||||
name: () => "Native",
|
|
||||||
selectable: { type: "selectable" },
|
public interceptorID = "native" // TODO: i18n this
|
||||||
runRequest(req) {
|
|
||||||
|
public name = () => "Native"
|
||||||
|
|
||||||
|
public selectable = { type: "selectable" as const }
|
||||||
|
|
||||||
|
public supportsCookies = true
|
||||||
|
|
||||||
|
public cookieJarService = this.bind(CookieJarService)
|
||||||
|
|
||||||
|
constructor() {
|
||||||
|
super()
|
||||||
|
}
|
||||||
|
|
||||||
|
public runRequest(req: any) {
|
||||||
const processedReq = preProcessRequest(req)
|
const processedReq = preProcessRequest(req)
|
||||||
|
|
||||||
|
const relevantCookies = this.cookieJarService.getCookiesForURL(
|
||||||
|
new URL(processedReq.url!)
|
||||||
|
)
|
||||||
|
|
||||||
|
processedReq.headers["Cookie"] = relevantCookies
|
||||||
|
.map((cookie) => `${cookie.name!}=${cookie.value!}`)
|
||||||
|
.join(";")
|
||||||
|
|
||||||
let cancelled = false
|
let cancelled = false
|
||||||
|
|
||||||
const checkCancelled = () => { return cancelled }
|
const checkCancelled = () => {
|
||||||
|
return cancelled
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
cancel: () => { cancelled = true },
|
cancel: () => {
|
||||||
|
cancelled = true
|
||||||
|
},
|
||||||
response: runRequest(processedReq, checkCancelled),
|
response: runRequest(processedReq, checkCancelled),
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
3
pnpm-lock.yaml
generated
3
pnpm-lock.yaml
generated
@@ -901,6 +901,9 @@ importers:
|
|||||||
buffer:
|
buffer:
|
||||||
specifier: ^6.0.3
|
specifier: ^6.0.3
|
||||||
version: 6.0.3
|
version: 6.0.3
|
||||||
|
dioc:
|
||||||
|
specifier: workspace:^
|
||||||
|
version: link:../dioc
|
||||||
environments.api:
|
environments.api:
|
||||||
specifier: link:@platform/environments/environments.api
|
specifier: link:@platform/environments/environments.api
|
||||||
version: link:@platform/environments/environments.api
|
version: link:@platform/environments/environments.api
|
||||||
|
|||||||
Reference in New Issue
Block a user