From 8201cb2701dff7cda30de28de57d6b620c79d7d4 Mon Sep 17 00:00:00 2001 From: spencerwooo Date: Wed, 26 Jan 2022 15:32:20 +0800 Subject: [PATCH] defining all available types (files and folders) --- components/FileListing.tsx | 26 +++++++------- types/index.d.ts | 73 ++++++++++++++++++++------------------ 2 files changed, 52 insertions(+), 47 deletions(-) diff --git a/components/FileListing.tsx b/components/FileListing.tsx index 2078817..9307e8e 100644 --- a/components/FileListing.tsx +++ b/components/FileListing.tsx @@ -37,6 +37,8 @@ import DefaultPreview from './previews/DefaultPreview' import { DownloadBtnContainer, PreviewContainer } from './previews/Containers' import DownloadButtonGroup from './DownloadBtnGtoup' +import { OdFileObject, OdFolderObject } from '../types' + // Disabling SSR for some previews (image gallery view, and PDF view) const ReactViewer = dynamic(() => import('react-viewer'), { ssr: false }) const EPUBPreview = dynamic(() => import('./previews/EPUBPreview'), { ssr: false }) @@ -57,9 +59,7 @@ const queryToPath = (query?: ParsedUrlQuery) => { return '/' } -const FileListItem: FC<{ - fileContent: { id: string; name: string; size: number; file: Object; lastModifiedDateTime: string, video: any } -}> = ({ fileContent: c }) => { +const FileListItem: FC<{ fileContent: OdFolderObject['value'][number] }> = ({ fileContent: c }) => { const emojiIcon = emojiRegex().exec(c.name) const renderEmoji = emojiIcon && !emojiIcon.index @@ -207,12 +207,12 @@ const FileListing: FC<{ query?: ParsedUrlQuery }> = ({ query }) => { // README rendering preparations let renderReadme = false - let readmeFile = null + let readmeFile = {} // Expand list of API returns into flattened file data - const children = [].concat(...responses.map(r => r.folder.value)) + const children = [].concat(...responses.map(r => r.folder.value)) as OdFolderObject['value'] - children.forEach((c: any) => { + children.forEach(c => { if (fileIsImage(c.name)) { imagesInFolder.push({ src: c['@microsoft.graph.downloadUrl'], @@ -230,11 +230,11 @@ const FileListing: FC<{ query?: ParsedUrlQuery }> = ({ query }) => { }) // Filtered file list helper - const getFiles = () => children.filter((c: any) => !c.folder && c.name !== '.password') + const getFiles = () => children.filter(c => !c.folder && c.name !== '.password') // File selection const genTotalSelected = (selected: { [key: string]: boolean }) => { - const selectInfo = getFiles().map((c: any) => Boolean(selected[c.id])) + const selectInfo = getFiles().map(c => Boolean(selected[c.id])) const [hasT, hasF] = [selectInfo.some(i => i), selectInfo.some(i => !i)] return hasT && hasF ? 1 : !hasF ? 2 : 0 } @@ -256,7 +256,7 @@ const FileListing: FC<{ query?: ParsedUrlQuery }> = ({ query }) => { setSelected({}) setTotalSelected(0) } else { - setSelected(Object.fromEntries(getFiles().map((c: any) => [c.id, true]))) + setSelected(Object.fromEntries(getFiles().map(c => [c.id, true]))) setTotalSelected(2) } } @@ -266,8 +266,8 @@ const FileListing: FC<{ query?: ParsedUrlQuery }> = ({ query }) => { const folderName = path.substring(path.lastIndexOf('/') + 1) const folder = folderName ? decodeURIComponent(folderName) : undefined const files = getFiles() - .filter((c: any) => selected[c.id]) - .map((c: any) => ({ name: c.name, url: c['@microsoft.graph.downloadUrl'] })) + .filter(c => selected[c.id]) + .map(c => ({ name: c.name, url: c['@microsoft.graph.downloadUrl'] })) if (files.length == 1) { const el = document.createElement('a') @@ -398,7 +398,7 @@ const FileListing: FC<{ query?: ParsedUrlQuery }> = ({ query }) => { /> )} - {children.map((c: any) => ( + {children.map(c => (
= ({ query }) => { } if ('file' in responses[0] && responses.length === 1) { - const { file } = responses[0] + const file = responses[0].file as OdFileObject const downloadUrl = file['@microsoft.graph.downloadUrl'] const fileName = file.name const fileExtension = fileName.slice(((fileName.lastIndexOf('.') - 1) >>> 0) + 2).toLowerCase() diff --git a/types/index.d.ts b/types/index.d.ts index 162cd7d..3581a68 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -1,47 +1,52 @@ -export type OdFileObject = { +// API response object for /api?path=, this may return either a file or a folder. +// Pagination is also declared here with the 'next' parameter. +export declare type OdAPIResponse = { file?: OdFileObject; folder?: OdFolderObject; next?: string } +// A folder object returned from the OneDrive API. This contains the parameter 'value', which is an array of items +// inside the folder. The items may also be either files or folders. +export declare type OdFolderObject = { + '@odata.count': number + '@odata.context': string + '@odata.nextLink'?: string + value: Array<{ + '@microsoft.graph.downloadUrl': string + id: string + name: string + size: number + lastModifiedDateTime: string + file?: { mimeType: string; hashes: { quickXorHash: string; sha1Hash?: string; sha256Hash?: string } } + folder?: { childCount: number; view: { sortBy: string; sortOrder: 'ascending'; viewType: 'thumbnails' } } + video?: OdVideoFile + }> +} +// A file object returned from the OneDrive API. This object may contain 'video' if the file is a video. +export declare type OdFileObject = { '@microsoft.graph.downloadUrl': string + '@odata.context': string name: string size: number id: string lastModifiedDateTime: string - file: { - mimeType: string - hashes: { - quickXorHash: string - sha1Hash: string - sha256Hash: string - } - }, - video: any // Check if field exists only currently + file: { mimeType: string; hashes: { quickXorHash: string; sha1Hash?: string; sha256Hash?: string } } + video?: OdVideoFile } - -export type OdFolderObject = { - '@odata.count': number - value: Array<{ - id: string - name: string - lastModifiedDateTime: string - size: number - folder: { - childCount: number - view: { - sortBy: 'name' - sortOrder: 'ascending' - viewType: 'thumbnails' - } - } - }> +// A representation of a OneDrive video file. All fields are declared here, but we mainly use 'width' and 'height'. +export declare type OdVideoFile = { + width: number + height: number + duration: number + bitrate: number + frameRate: number + audioBitsPerSample: number + audioChannels: number + audioFormat: string + audioSamplesPerSecond: number } - -export type OdSearchResult = Array<{ +// API response object for /api/search?q=. Likewise, this array of items may also contain either files or folders. +export declare type OdSearchResult = Array<{ id: string name: string file?: OdFileObject folder?: OdFolderObject path: string - parentReference: { - id: string - name: string - path: string - } + parentReference: { id: string; name: string; path?: string } }>