Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(api): finalize export type usage #1847

Merged
merged 1 commit into from
May 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .changes/api-export-type.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
"api": patch
---

Use `export type` to export TS types.
Use `export type` to export TS types, enums and interfaces.
2 changes: 1 addition & 1 deletion core/tauri/scripts/bundle.js

Large diffs are not rendered by default.

8 changes: 5 additions & 3 deletions tooling/api/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import { invokeTauriCommand } from './helpers/tauri'

export interface ArgMatch {
interface ArgMatch {
/**
* string if takes value
* boolean if flag
Expand All @@ -22,12 +22,12 @@ export interface ArgMatch {
occurrences: number
}

export interface SubcommandMatch {
interface SubcommandMatch {
name: string
matches: CliMatches
}

export interface CliMatches {
interface CliMatches {
args: { [name: string]: ArgMatch }
subcommand: SubcommandMatch | null
}
Expand All @@ -46,4 +46,6 @@ async function getMatches(): Promise<CliMatches> {
})
}

export type { ArgMatch, SubcommandMatch, CliMatches }

export { getMatches }
8 changes: 5 additions & 3 deletions tooling/api/src/dialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import { invokeTauriCommand } from './helpers/tauri'

/** Extension filters for the file dialog. */
export interface DialogFilter {
interface DialogFilter {
/** Filter name. */
name: string
/**
Expand All @@ -24,7 +24,7 @@ export interface DialogFilter {
}

/** Options for the open dialog. */
export interface OpenDialogOptions {
interface OpenDialogOptions {
/** The filters of the dialog. */
filters?: DialogFilter[]
/** Initial directory or file path. It must exist. */
Expand All @@ -36,7 +36,7 @@ export interface OpenDialogOptions {
}

/** Options for the save dialog. */
export interface SaveDialogOptions {
interface SaveDialogOptions {
/** The filters of the dialog. */
filters?: DialogFilter[]
/** Initial directory or file path. It must exist. */
Expand Down Expand Up @@ -83,4 +83,6 @@ async function save(options: SaveDialogOptions = {}): Promise<string> {
})
}

export type { DialogFilter, OpenDialogOptions, SaveDialogOptions }

export { open, save }
8 changes: 5 additions & 3 deletions tooling/api/src/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { invokeTauriCommand } from './helpers/tauri'
import { emit as emitEvent } from './helpers/event'
import { transformCallback } from './tauri'

export interface Event<T> {
interface Event<T> {
/** Event name */
event: string
/** Event identifier used to unlisten */
Expand All @@ -20,9 +20,9 @@ export interface Event<T> {
payload: T
}

export type EventCallback<T> = (event: Event<T>) => void
type EventCallback<T> = (event: Event<T>) => void

export type UnlistenFn = () => void
type UnlistenFn = () => void

/**
* Unregister the event listener associated with the given id.
Expand Down Expand Up @@ -92,4 +92,6 @@ async function emit(event: string, payload?: string): Promise<void> {
return emitEvent(event, undefined, payload)
}

export type { Event, EventCallback, UnlistenFn }

export { listen, once, emit }
18 changes: 13 additions & 5 deletions tooling/api/src/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,26 @@ export enum BaseDirectory {
Current
}

export interface FsOptions {
interface FsOptions {
dir?: BaseDirectory
}

export interface FsDirOptions {
interface FsDirOptions {
dir?: BaseDirectory
recursive?: boolean
}

export interface FsTextFileOption {
interface FsTextFileOption {
path: string
contents: string
}

export interface FsBinaryFileOption {
interface FsBinaryFileOption {
path: string
contents: ArrayBuffer
}

export interface FileEntry {
interface FileEntry {
path: string
/**
* Name of the directory/file
Expand Down Expand Up @@ -332,6 +332,14 @@ async function renameFile(
})
}

export type {
FsOptions,
FsDirOptions,
FsTextFileOption,
FsBinaryFileOption,
FileEntry
}

export {
BaseDirectory as Dir,
readTextFile,
Expand Down
10 changes: 7 additions & 3 deletions tooling/api/src/helpers/tauri.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import { invoke } from '../tauri'

export type TauriModule =
type TauriModule =
| 'App'
| 'Fs'
| 'Window'
Expand All @@ -20,11 +20,15 @@ export type TauriModule =
| 'GlobalShortcut'
| 'Process'

export interface TauriCommand {
interface TauriCommand {
__tauriModule: TauriModule
[key: string]: unknown
}

export async function invokeTauriCommand<T>(command: TauriCommand): Promise<T> {
async function invokeTauriCommand<T>(command: TauriCommand): Promise<T> {
return invoke('tauri', command)
}

export type { TauriModule, TauriCommand }

export { invokeTauriCommand }
35 changes: 23 additions & 12 deletions tooling/api/src/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,26 @@

import { invokeTauriCommand } from './helpers/tauri'

export interface ClientOptions {
interface ClientOptions {
maxRedirections: number
connectTimeout: number
}

export enum ResponseType {
enum ResponseType {
JSON = 1,
Text = 2,
Binary = 3
}

export type Part = 'string' | number[]
type Part = 'string' | number[]

/** The body object to be used on POST and PUT requests. */
export class Body {
class Body {
type: string
payload: unknown

/** @ignore */
constructor(type: string, payload: unknown) {
private constructor(type: string, payload: unknown) {
this.type = type
this.payload = payload
}
Expand Down Expand Up @@ -79,7 +79,7 @@ export class Body {
}

/** The request HTTP verb. */
export type HttpVerb =
type HttpVerb =
| 'GET'
| 'POST'
| 'PUT'
Expand All @@ -91,7 +91,7 @@ export type HttpVerb =
| 'TRACE'

/** Options object sent to the backend. */
export interface HttpOptions {
interface HttpOptions {
method: HttpVerb
url: string
headers?: Record<string, any>
Expand All @@ -102,12 +102,12 @@ export interface HttpOptions {
}

/** Request options. */
export type RequestOptions = Omit<HttpOptions, 'method' | 'url'>
type RequestOptions = Omit<HttpOptions, 'method' | 'url'>
/** Options for the `fetch` API. */
export type FetchOptions = Omit<HttpOptions, 'url'>
type FetchOptions = Omit<HttpOptions, 'url'>

/** Response object. */
export interface Response<T> {
interface Response<T> {
/** The request URL. */
url: string
/** The response status code. */
Expand All @@ -118,7 +118,7 @@ export interface Response<T> {
data: T
}

export class Client {
class Client {
id: number
/** @ignore */
constructor(id: number) {
Expand Down Expand Up @@ -286,4 +286,15 @@ async function fetch<T>(
})
}

export { getClient, fetch }
export type {
ClientOptions,
ResponseType,
Part,
HttpVerb,
HttpOptions,
RequestOptions,
FetchOptions,
Response
}

export { getClient, fetch, Body, Client }
6 changes: 4 additions & 2 deletions tooling/api/src/notification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { invokeTauriCommand } from './helpers/tauri'
/**
* Options to send a notification.
*/
export interface Options {
interface Options {
/** Notification title. */
title: string
/** Optional notification body. */
Expand All @@ -22,7 +22,7 @@ export interface Options {
}

/** Possible permission values. */
export type Permission = 'granted' | 'denied' | 'default'
type Permission = 'granted' | 'denied' | 'default'

/**
* Checks if the permission to send notifications is granted.
Expand Down Expand Up @@ -65,4 +65,6 @@ function sendNotification(options: Options | string): void {
}
}

export type { Options, Permission }

export { sendNotification, requestPermission, isPermissionGranted }
4 changes: 3 additions & 1 deletion tooling/api/src/tauri.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function transformCallback(
}

/** Command arguments. */
export interface InvokeArgs {
interface InvokeArgs {
[key: string]: unknown
}

Expand Down Expand Up @@ -84,4 +84,6 @@ async function invoke<T>(cmd: string, args: InvokeArgs = {}): Promise<T> {
})
}

export type { InvokeArgs }

export { transformCallback, invoke }
16 changes: 10 additions & 6 deletions tooling/api/src/updater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@

import { once, listen, emit, UnlistenFn } from './event'

export type UpdateStatus = 'PENDING' | 'ERROR' | 'DONE' | 'UPTODATE'
type UpdateStatus = 'PENDING' | 'ERROR' | 'DONE' | 'UPTODATE'

export interface UpdateStatusResult {
interface UpdateStatusResult {
error?: string
status: UpdateStatus
}

export interface UpdateManifest {
interface UpdateManifest {
version: string
date: string
body: string
}

export interface UpdateResult {
interface UpdateResult {
manifest?: UpdateManifest
shouldUpdate: boolean
}
Expand All @@ -32,7 +32,7 @@ export interface UpdateResult {
*
* @return A promise indicating the success or failure of the operation.
*/
export async function installUpdate(): Promise<void> {
async function installUpdate(): Promise<void> {
let unlistenerFn: UnlistenFn | undefined

function cleanListener(): void {
Expand Down Expand Up @@ -84,7 +84,7 @@ export async function installUpdate(): Promise<void> {
*
* @return Promise resolving to the update status.
*/
export async function checkUpdate(): Promise<UpdateResult> {
async function checkUpdate(): Promise<UpdateResult> {
let unlistenerFn: UnlistenFn | undefined

function cleanListener(): void {
Expand Down Expand Up @@ -147,3 +147,7 @@ export async function checkUpdate(): Promise<UpdateResult> {
})
})
}

export type { UpdateStatus, UpdateStatusResult, UpdateManifest, UpdateResult }

export { installUpdate, checkUpdate }
7 changes: 4 additions & 3 deletions tooling/api/src/window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ class WebviewWindow extends WebviewWindowHandle {
/**
* Manage the current window object.
*/
export class WindowManager {
class WindowManager {
// Getters
/** The scale factor that can be used to map physical pixels to logical pixels. */
async scaleFactor(): Promise<number> {
Expand Down Expand Up @@ -703,7 +703,7 @@ export class WindowManager {
const appWindow = new WindowManager()

/** Configuration for the window to create. */
export interface WindowOptions {
interface WindowOptions {
/**
* Remote URL or local file path to open, e.g. `https://github.com/tauri-apps` or `path/to/page.html`.
*/
Expand Down Expand Up @@ -781,6 +781,7 @@ async function availableMonitors(): Promise<Monitor[]> {
export {
WebviewWindow,
WebviewWindowHandle,
WindowManager,
getCurrent,
getAll,
appWindow,
Expand All @@ -793,4 +794,4 @@ export {
availableMonitors
}

export type { Monitor }
export type { Monitor, WindowOptions }