feat: desktop app
Co-authored-by: Vivek R <123vivekr@gmail.com> Co-authored-by: Liyas Thomas <liyascthomas@gmail.com>
This commit is contained in:
@@ -9,12 +9,14 @@ import { AnalyticsPlatformDef } from "./analytics"
|
||||
import { InterceptorsPlatformDef } from "./interceptors"
|
||||
import { HoppModule } from "~/modules"
|
||||
import { InspectorsPlatformDef } from "./inspectors"
|
||||
import { IOPlatformDef } from "./io"
|
||||
|
||||
export type PlatformDef = {
|
||||
ui?: UIPlatformDef
|
||||
addedHoppModules?: HoppModule[]
|
||||
auth: AuthPlatformDef
|
||||
analytics?: AnalyticsPlatformDef
|
||||
io: IOPlatformDef
|
||||
sync: {
|
||||
environments: EnvironmentsPlatformDef
|
||||
collections: CollectionsPlatformDef
|
||||
@@ -27,6 +29,12 @@ export type PlatformDef = {
|
||||
platformFeatureFlags: {
|
||||
exportAsGIST: boolean
|
||||
hasTelemetry: boolean
|
||||
|
||||
/**
|
||||
* Whether the platform supports cookies (affects whether the cookies footer item is shown)
|
||||
* If a value is not given, then the value is assumed to be false
|
||||
*/
|
||||
cookiesEnabled?: boolean
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
84
packages/hoppscotch-common/src/platform/io.ts
Normal file
84
packages/hoppscotch-common/src/platform/io.ts
Normal file
@@ -0,0 +1,84 @@
|
||||
/**
|
||||
* Defines how to save a file to the user's filesystem.
|
||||
*/
|
||||
export type SaveFileWithDialogOptions = {
|
||||
/**
|
||||
* The data to be saved
|
||||
*/
|
||||
data: string | ArrayBuffer
|
||||
|
||||
/**
|
||||
* The suggested filename for the file. This name will be shown in the
|
||||
* save dialog by default when a save is initiated.
|
||||
*/
|
||||
suggestedFilename: string
|
||||
|
||||
/**
|
||||
* The content type mime type of the data to be saved.
|
||||
*
|
||||
* NOTE: The usage of this data might be platform dependent.
|
||||
* For example, this field is used in the web, but not in the desktop app.
|
||||
*/
|
||||
contentType: string
|
||||
|
||||
/**
|
||||
* Defines the filters (like in Windows, on the right side, where you can
|
||||
* select the file type) for the file dialog.
|
||||
*
|
||||
* NOTE: The usage of this data might be platform dependent.
|
||||
* For example, this field is used in the web, but not in the desktop app.
|
||||
*/
|
||||
filters?: Array<{
|
||||
/**
|
||||
* The name of the filter (in Windows, if the filter looks
|
||||
* like "Images (*.png, *.jpg)", the name would be "Images")
|
||||
*/
|
||||
name: string
|
||||
|
||||
/**
|
||||
* The array of extensions that are supported, without the dot.
|
||||
*/
|
||||
extensions: string[]
|
||||
}>
|
||||
}
|
||||
|
||||
export type SaveFileResponse =
|
||||
| {
|
||||
/**
|
||||
* The implementation was unable to determine the status of the save operation.
|
||||
* This cannot be considered a success or a failure and should be handled as an uncertainity.
|
||||
* The browser standard implementation (std) returns this value as there is no way to
|
||||
* check if the user downloaded the file or not.
|
||||
*/
|
||||
type: "unknown"
|
||||
}
|
||||
| {
|
||||
/**
|
||||
* The result is known and the user cancelled the save.
|
||||
*/
|
||||
type: "cancelled"
|
||||
}
|
||||
| {
|
||||
/**
|
||||
* The result is known and the user saved the file.
|
||||
*/
|
||||
type: "saved"
|
||||
|
||||
/**
|
||||
* The full path of where the file was saved
|
||||
*/
|
||||
path: string
|
||||
}
|
||||
|
||||
/**
|
||||
* Platform definitions for how to handle IO operations.
|
||||
*/
|
||||
export type IOPlatformDef = {
|
||||
/**
|
||||
* Defines how to save a file to the user's filesystem.
|
||||
* The expected behaviour is for the browser to show a prompt to save the file.
|
||||
*/
|
||||
saveFileWithDialog: (
|
||||
opts: SaveFileWithDialogOptions
|
||||
) => Promise<SaveFileResponse>
|
||||
}
|
||||
37
packages/hoppscotch-common/src/platform/std/io.ts
Normal file
37
packages/hoppscotch-common/src/platform/std/io.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import { IOPlatformDef } from "../io"
|
||||
import { pipe } from "fp-ts/function"
|
||||
import * as S from "fp-ts/string"
|
||||
import * as RNEA from "fp-ts/ReadonlyNonEmptyArray"
|
||||
|
||||
/**
|
||||
* Implementation for how to handle IO operations in the browser.
|
||||
*/
|
||||
export const browserIODef: IOPlatformDef = {
|
||||
saveFileWithDialog(opts) {
|
||||
const file = new Blob([opts.data], { type: opts.contentType })
|
||||
const a = document.createElement("a")
|
||||
const url = URL.createObjectURL(file)
|
||||
|
||||
a.href = url
|
||||
a.download = pipe(
|
||||
url,
|
||||
S.split("/"),
|
||||
RNEA.last,
|
||||
S.split("#"),
|
||||
RNEA.head,
|
||||
S.split("?"),
|
||||
RNEA.head
|
||||
)
|
||||
|
||||
document.body.appendChild(a)
|
||||
a.click()
|
||||
|
||||
setTimeout(() => {
|
||||
document.body.removeChild(a)
|
||||
URL.revokeObjectURL(url)
|
||||
}, 1000)
|
||||
|
||||
// Browsers provide no way for us to know the save went successfully.
|
||||
return Promise.resolve({ type: "unknown" })
|
||||
},
|
||||
}
|
||||
Reference in New Issue
Block a user