Add types to fetch,toast,bootstrap,svg (#31627)
Reduce `tsc` error count by 53. None of the changes has any runtime effect.
This commit is contained in:
parent
cabcca3d81
commit
930ca92d7c
6 changed files with 56 additions and 33 deletions
|
@ -1,4 +1,5 @@
|
|||
import {isObject} from '../utils.ts';
|
||||
import type {RequestData, RequestOpts} from '../types.ts';
|
||||
|
||||
const {csrfToken} = window.config;
|
||||
|
||||
|
@ -8,8 +9,9 @@ const safeMethods = new Set(['GET', 'HEAD', 'OPTIONS', 'TRACE']);
|
|||
// fetch wrapper, use below method name functions and the `data` option to pass in data
|
||||
// which will automatically set an appropriate headers. For json content, only object
|
||||
// and array types are currently supported.
|
||||
export function request(url, {method = 'GET', data, headers = {}, ...other} = {}) {
|
||||
let body, contentType;
|
||||
export function request(url: string, {method = 'GET', data, headers = {}, ...other}: RequestOpts = {}) {
|
||||
let body: RequestData;
|
||||
let contentType: string;
|
||||
if (data instanceof FormData || data instanceof URLSearchParams) {
|
||||
body = data;
|
||||
} else if (isObject(data) || Array.isArray(data)) {
|
||||
|
@ -34,8 +36,8 @@ export function request(url, {method = 'GET', data, headers = {}, ...other} = {}
|
|||
});
|
||||
}
|
||||
|
||||
export const GET = (url, opts) => request(url, {method: 'GET', ...opts});
|
||||
export const POST = (url, opts) => request(url, {method: 'POST', ...opts});
|
||||
export const PATCH = (url, opts) => request(url, {method: 'PATCH', ...opts});
|
||||
export const PUT = (url, opts) => request(url, {method: 'PUT', ...opts});
|
||||
export const DELETE = (url, opts) => request(url, {method: 'DELETE', ...opts});
|
||||
export const GET = (url: string, opts?: RequestOpts) => request(url, {method: 'GET', ...opts});
|
||||
export const POST = (url: string, opts?: RequestOpts) => request(url, {method: 'POST', ...opts});
|
||||
export const PATCH = (url: string, opts?: RequestOpts) => request(url, {method: 'PATCH', ...opts});
|
||||
export const PUT = (url: string, opts?: RequestOpts) => request(url, {method: 'PUT', ...opts});
|
||||
export const DELETE = (url: string, opts?: RequestOpts) => request(url, {method: 'DELETE', ...opts});
|
||||
|
|
|
@ -2,8 +2,19 @@ import {htmlEscape} from 'escape-goat';
|
|||
import {svg} from '../svg.ts';
|
||||
import {animateOnce, showElem} from '../utils/dom.ts';
|
||||
import Toastify from 'toastify-js'; // don't use "async import", because when network error occurs, the "async import" also fails and nothing is shown
|
||||
import type {Intent} from '../types.ts';
|
||||
import type {SvgName} from '../svg.ts';
|
||||
import type {Options} from 'toastify-js';
|
||||
|
||||
const levels = {
|
||||
type ToastLevels = {
|
||||
[intent in Intent]: {
|
||||
icon: SvgName,
|
||||
background: string,
|
||||
duration: number,
|
||||
}
|
||||
}
|
||||
|
||||
const levels: ToastLevels = {
|
||||
info: {
|
||||
icon: 'octicon-check',
|
||||
background: 'var(--color-green)',
|
||||
|
@ -21,8 +32,13 @@ const levels = {
|
|||
},
|
||||
};
|
||||
|
||||
type ToastOpts = {
|
||||
useHtmlBody?: boolean,
|
||||
preventDuplicates?: boolean,
|
||||
} & Options;
|
||||
|
||||
// See https://github.com/apvarun/toastify-js#api for options
|
||||
function showToast(message, level, {gravity, position, duration, useHtmlBody, preventDuplicates = true, ...other} = {}) {
|
||||
function showToast(message: string, level: Intent, {gravity, position, duration, useHtmlBody, preventDuplicates = true, ...other}: ToastOpts = {}) {
|
||||
const body = useHtmlBody ? String(message) : htmlEscape(message);
|
||||
const key = `${level}-${body}`;
|
||||
|
||||
|
@ -59,14 +75,14 @@ function showToast(message, level, {gravity, position, duration, useHtmlBody, pr
|
|||
return toast;
|
||||
}
|
||||
|
||||
export function showInfoToast(message, opts) {
|
||||
export function showInfoToast(message: string, opts?: ToastOpts) {
|
||||
return showToast(message, 'info', opts);
|
||||
}
|
||||
|
||||
export function showWarningToast(message, opts) {
|
||||
export function showWarningToast(message: string, opts?: ToastOpts) {
|
||||
return showToast(message, 'warning', opts);
|
||||
}
|
||||
|
||||
export function showErrorToast(message, opts) {
|
||||
export function showErrorToast(message: string, opts?: ToastOpts) {
|
||||
return showToast(message, 'error', opts);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue