@ismou-workspace/drive-contracts 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +15 -2
- package/dist/index.d.ts +1 -0
- package/dist/index.js +2 -0
- package/dist/item.type.d.ts +194 -0
- package/dist/item.type.js +342 -0
- package/package.json +35 -3
package/README.md
CHANGED
|
@@ -1,3 +1,16 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @ismou-workspace/drive-contracts
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The `drive.item` entity type of Ismou's Drive app (files and folders), and the DTOs of its API.
|
|
4
|
+
|
|
5
|
+
Published by the [drive](https://github.com/ismou-workspace/drive) app for the apps that use its entities: apps never know each other, the entities come through the core, and these types describe them.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
pnpm add @ismou-workspace/drive-contracts
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Documentation
|
|
14
|
+
|
|
15
|
+
- [The platform's documentation](https://github.com/ismou-workspace/sdk/tree/main/docs): architecture, conventions, design system.
|
|
16
|
+
- [Source](https://github.com/ismou-workspace/drive/tree/main/contracts)
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { DRIVE_FILE_KIND_NAMES, DRIVE_FILE_KINDS, DRIVE_FILE_PART, DRIVE_ITEM_ACTIONS, DRIVE_ITEM_KINDS, DRIVE_ITEM_OPERATIONS, DRIVE_ITEM_TYPE_ID, DRIVE_LIST_PARAMS, DRIVE_SEARCH_LIMIT, type DriveFileKind, type DriveItem, type DriveItemAncestors, DriveItemAncestorsSchema, type DriveItemCopy, type DriveItemCopyResult, DriveItemCopyResultSchema, DriveItemCopySchema, type DriveItemKind, DriveItemKindSchema, type DriveItemListParams, DriveItemListParamsSchema, DriveItemNameSchema, type DriveItemProvenance, DriveItemProvenanceSchema, DriveItemSchema, type DriveItemUpdate, DriveItemUpdateSchema, driveItemKind, driveItemType, type FolderCreation, FolderCreationSchema, isTrashed, MAX_COPIED_ITEMS, } from './item.type.ts';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
/* The `drive.item` entity type (docs/architecture/entities/). */
|
|
2
|
+
export { DRIVE_FILE_KIND_NAMES, DRIVE_FILE_KINDS, DRIVE_FILE_PART, DRIVE_ITEM_ACTIONS, DRIVE_ITEM_KINDS, DRIVE_ITEM_OPERATIONS, DRIVE_ITEM_TYPE_ID, DRIVE_LIST_PARAMS, DRIVE_SEARCH_LIMIT, DriveItemAncestorsSchema, DriveItemCopyResultSchema, DriveItemCopySchema, DriveItemKindSchema, DriveItemListParamsSchema, DriveItemNameSchema, DriveItemProvenanceSchema, DriveItemSchema, DriveItemUpdateSchema, driveItemKind, driveItemType, FolderCreationSchema, isTrashed, MAX_COPIED_ITEMS, } from './item.type.js';
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
export declare const DRIVE_ITEM_TYPE_ID = "drive.item";
|
|
3
|
+
export declare const DRIVE_ITEM_KINDS: readonly ['folder', 'file'];
|
|
4
|
+
export declare const DriveItemKindSchema: z.ZodEnum<{
|
|
5
|
+
file: "file";
|
|
6
|
+
folder: "folder";
|
|
7
|
+
}>;
|
|
8
|
+
export type DriveItemKind = z.infer<typeof DriveItemKindSchema>;
|
|
9
|
+
/** The name of a file or a folder, as shown and as downloaded. */
|
|
10
|
+
export declare const DriveItemNameSchema: z.ZodString;
|
|
11
|
+
/** Who created an item (uploaded the file, made the folder), and through which app. */
|
|
12
|
+
export declare const DriveItemProvenanceSchema: z.ZodObject<{
|
|
13
|
+
accountId: z.ZodString;
|
|
14
|
+
appId: z.ZodString;
|
|
15
|
+
}, z.core.$strip>;
|
|
16
|
+
export type DriveItemProvenance = z.infer<typeof DriveItemProvenanceSchema>;
|
|
17
|
+
/** An item's data (its id, folder, owner and dates are the envelope's). */
|
|
18
|
+
export declare const DriveItemSchema: z.ZodObject<{
|
|
19
|
+
kind: z.ZodEnum<{
|
|
20
|
+
file: "file";
|
|
21
|
+
folder: "folder";
|
|
22
|
+
}>;
|
|
23
|
+
name: z.ZodString;
|
|
24
|
+
contentType: z.ZodNullable<z.ZodString>;
|
|
25
|
+
size: z.ZodNullable<z.ZodInt>;
|
|
26
|
+
hasThumbnail: z.ZodBoolean;
|
|
27
|
+
hasFolders: z.ZodBoolean;
|
|
28
|
+
trashedAt: z.ZodNullable<z.ZodISODateTime>;
|
|
29
|
+
createdBy: z.ZodObject<{
|
|
30
|
+
accountId: z.ZodString;
|
|
31
|
+
appId: z.ZodString;
|
|
32
|
+
}, z.core.$strip>;
|
|
33
|
+
}, z.core.$strip>;
|
|
34
|
+
export type DriveItem = z.infer<typeof DriveItemSchema>;
|
|
35
|
+
/** Whether the item is an entry of the trash (moved there by itself). */
|
|
36
|
+
export declare const isTrashed: (item: Pick<DriveItem, 'trashedAt'>) => boolean;
|
|
37
|
+
/**
|
|
38
|
+
* Creating a folder (a JSON `create`), in a folder (`?parent=`, the folder's owner owning it) or
|
|
39
|
+
* at the top of the owner's drive.
|
|
40
|
+
*/
|
|
41
|
+
export declare const FolderCreationSchema: z.ZodObject<{
|
|
42
|
+
name: z.ZodString;
|
|
43
|
+
ifExists: z.ZodDefault<z.ZodEnum<{
|
|
44
|
+
fail: "fail";
|
|
45
|
+
reuse: "reuse";
|
|
46
|
+
}>>;
|
|
47
|
+
}, z.core.$strip>;
|
|
48
|
+
export type FolderCreation = z.input<typeof FolderCreationSchema>;
|
|
49
|
+
/**
|
|
50
|
+
* Creating a file is a multipart `create`: the file in this part, named by its file name (only
|
|
51
|
+
* its last segment: browsers name the files of a dropped folder by their path in it). A name
|
|
52
|
+
* already taken in the folder gets a number: "report (2).pdf". At most the core storage's size.
|
|
53
|
+
*/
|
|
54
|
+
export declare const DRIVE_FILE_PART = "file";
|
|
55
|
+
/**
|
|
56
|
+
* Changing an item (the body of `update`): renaming it, or moving it to the top of its owner's
|
|
57
|
+
* drive (`root`, the owner only). Moving it into another folder of its owner is `?parent=`.
|
|
58
|
+
* Names are unique in a folder, whatever the case and the accents.
|
|
59
|
+
*/
|
|
60
|
+
export declare const DriveItemUpdateSchema: z.ZodObject<{
|
|
61
|
+
name: z.ZodOptional<z.ZodString>;
|
|
62
|
+
root: z.ZodOptional<z.ZodLiteral<true>>;
|
|
63
|
+
}, z.core.$strip>;
|
|
64
|
+
export type DriveItemUpdate = z.input<typeof DriveItemUpdateSchema>;
|
|
65
|
+
/**
|
|
66
|
+
* What a list takes (none: the top of what the call reaches, the owner's top items, or the
|
|
67
|
+
* shared item itself within a share). Every list leaves the trash out, but `trashed`.
|
|
68
|
+
*/
|
|
69
|
+
export declare const DRIVE_LIST_PARAMS: {
|
|
70
|
+
/**
|
|
71
|
+
* The items of this folder. Not `parent`: the core keeps that name for creations and moves.
|
|
72
|
+
* A folder out of the call's reach lists nothing.
|
|
73
|
+
*/
|
|
74
|
+
readonly folder: 'folder';
|
|
75
|
+
/** Every reachable item whose name holds it, whatever the case and the accents. */
|
|
76
|
+
readonly search: 'search';
|
|
77
|
+
/** `true`: the entries of the trash (within a share, those the acting account trashed). */
|
|
78
|
+
readonly trashed: 'trashed';
|
|
79
|
+
};
|
|
80
|
+
export declare const DriveItemListParamsSchema: z.ZodObject<{
|
|
81
|
+
folder: z.ZodOptional<z.ZodString>;
|
|
82
|
+
search: z.ZodOptional<z.ZodString>;
|
|
83
|
+
trashed: z.ZodPipe<z.ZodOptional<z.ZodEnum<{
|
|
84
|
+
false: "false";
|
|
85
|
+
true: "true";
|
|
86
|
+
}>>, z.ZodTransform<boolean, "false" | "true" | undefined>>;
|
|
87
|
+
}, z.core.$strip>;
|
|
88
|
+
export type DriveItemListParams = z.input<typeof DriveItemListParamsSchema>;
|
|
89
|
+
/** Most results a search answers. */
|
|
90
|
+
export declare const DRIVE_SEARCH_LIMIT = 100;
|
|
91
|
+
/** The item's own operations, and what they take and answer. */
|
|
92
|
+
export declare const DRIVE_ITEM_OPERATIONS: {
|
|
93
|
+
/**
|
|
94
|
+
* GET → the file's bytes: images and PDFs inline with their type, anything else as an
|
|
95
|
+
* attachment of no particular type. From the trash too.
|
|
96
|
+
*/
|
|
97
|
+
readonly content: 'content';
|
|
98
|
+
/** GET → a small WebP picture of an image file (`hasThumbnail`); 404 without one. */
|
|
99
|
+
readonly thumbnail: 'thumbnail';
|
|
100
|
+
/** GET → `DriveItemAncestors`: where it sits, as far as the call reaches. */
|
|
101
|
+
readonly ancestors: 'ancestors';
|
|
102
|
+
/** POST → 204: moves it to the trash, with everything below it. */
|
|
103
|
+
readonly trash: 'trash';
|
|
104
|
+
/**
|
|
105
|
+
* POST → 204: takes an entry of the trash back where it was (its folder must be out of the
|
|
106
|
+
* trash; a name taken there since gets a number).
|
|
107
|
+
*/
|
|
108
|
+
readonly restore: 'restore';
|
|
109
|
+
/**
|
|
110
|
+
* PUT multipart (`file`: the new bytes; its type kept) → 204: a new version of a file (an
|
|
111
|
+
* editor saving it). Its picture is made again when an app previews its kind.
|
|
112
|
+
*/
|
|
113
|
+
readonly replaceContent: 'replace-content';
|
|
114
|
+
/**
|
|
115
|
+
* POST `DriveItemCopy` → 201 `DriveItemCopyResult`: copies it, with everything live below it,
|
|
116
|
+
* into a folder of the same owner (or the top of the owner's drive), at most
|
|
117
|
+
* `MAX_COPIED_ITEMS` items; the copy takes a free name there. Copying to another owner, or
|
|
118
|
+
* another provider, is a `create` there.
|
|
119
|
+
*/
|
|
120
|
+
readonly copy: 'copy';
|
|
121
|
+
};
|
|
122
|
+
/**
|
|
123
|
+
* The folders above an item the call reaches, from the topmost down to its parent. Within a
|
|
124
|
+
* share, they stop at the shared folder; when the topmost one has a parent, it is out of reach.
|
|
125
|
+
*/
|
|
126
|
+
export declare const DriveItemAncestorsSchema: z.ZodObject<{
|
|
127
|
+
ancestors: z.ZodArray<z.ZodObject<{
|
|
128
|
+
id: z.ZodString;
|
|
129
|
+
name: z.ZodString;
|
|
130
|
+
}, z.core.$strip>>;
|
|
131
|
+
}, z.core.$strip>;
|
|
132
|
+
export type DriveItemAncestors = z.infer<typeof DriveItemAncestorsSchema>;
|
|
133
|
+
/** Where a copy goes: a folder of the item's owner, or the top of the owner's drive (`null`). */
|
|
134
|
+
export declare const DriveItemCopySchema: z.ZodObject<{
|
|
135
|
+
parentId: z.ZodNullable<z.ZodString>;
|
|
136
|
+
}, z.core.$strip>;
|
|
137
|
+
export type DriveItemCopy = z.infer<typeof DriveItemCopySchema>;
|
|
138
|
+
export declare const DriveItemCopyResultSchema: z.ZodObject<{
|
|
139
|
+
id: z.ZodString;
|
|
140
|
+
}, z.core.$strip>;
|
|
141
|
+
export type DriveItemCopyResult = z.infer<typeof DriveItemCopyResultSchema>;
|
|
142
|
+
/** The most items one copy makes (a folder and everything below it). */
|
|
143
|
+
export declare const MAX_COPIED_ITEMS = 1000;
|
|
144
|
+
/** The kinds of files apps handle apart, and the content types each covers. */
|
|
145
|
+
export declare const DRIVE_FILE_KINDS: {
|
|
146
|
+
readonly document: readonly ["application/vnd.openxmlformats-officedocument.wordprocessingml.document", "application/msword", "application/vnd.oasis.opendocument.text", "application/rtf", "text/plain"];
|
|
147
|
+
readonly spreadsheet: readonly ["application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "application/vnd.ms-excel", "application/vnd.oasis.opendocument.spreadsheet", "text/csv"];
|
|
148
|
+
readonly presentation: readonly ["application/vnd.openxmlformats-officedocument.presentationml.presentation", "application/vnd.ms-powerpoint", "application/vnd.oasis.opendocument.presentation"];
|
|
149
|
+
/** A fillable PDF: only ever created (a file of it is a `pdf`). */
|
|
150
|
+
readonly form: readonly ["application/pdf"];
|
|
151
|
+
readonly pdf: readonly ["application/pdf"];
|
|
152
|
+
readonly markdown: readonly ["text/markdown"];
|
|
153
|
+
readonly diagram: readonly ["application/vnd.ms-visio.drawing.main+xml", "application/vnd.visio", "application/vnd.oasis.opendocument.graphics"];
|
|
154
|
+
};
|
|
155
|
+
export type DriveFileKind = keyof typeof DRIVE_FILE_KINDS;
|
|
156
|
+
export declare const DRIVE_FILE_KIND_NAMES: DriveFileKind[];
|
|
157
|
+
/** The actions of `drive.item` other apps handle. */
|
|
158
|
+
export declare const DRIVE_ITEM_ACTIONS: {
|
|
159
|
+
/** A page showing the file (`read` on it); editing too when the account may write it. */
|
|
160
|
+
readonly open: 'open';
|
|
161
|
+
/** A server answering a picture of the file (`read` on it): PNG, JPEG or WebP. */
|
|
162
|
+
readonly preview: 'preview';
|
|
163
|
+
};
|
|
164
|
+
/**
|
|
165
|
+
* A file's kind, as handlers name it: from its name's extension, the one people see, else its
|
|
166
|
+
* content type; `form` aside (forms are PDFs once made). `undefined` for a folder or a file of no
|
|
167
|
+
* known kind.
|
|
168
|
+
*/
|
|
169
|
+
export declare function driveItemKind(item: Pick<DriveItem, 'kind' | 'name' | 'contentType'>): DriveFileKind | undefined;
|
|
170
|
+
export declare const driveItemType: {
|
|
171
|
+
id: string;
|
|
172
|
+
version: number;
|
|
173
|
+
parent: string | null;
|
|
174
|
+
titleFields: string[];
|
|
175
|
+
schema: Record<string, z.JSONType>;
|
|
176
|
+
permissions: Record<string, string | null>;
|
|
177
|
+
operations: Record<string, string>;
|
|
178
|
+
kinds: string[];
|
|
179
|
+
actions: Record<string, {
|
|
180
|
+
permission: string;
|
|
181
|
+
target: "page" | "server";
|
|
182
|
+
}>;
|
|
183
|
+
messages: Partial<Record<"en" | "fr", {
|
|
184
|
+
name: string;
|
|
185
|
+
plural: string;
|
|
186
|
+
permissions: Record<string, {
|
|
187
|
+
label: string;
|
|
188
|
+
description: string;
|
|
189
|
+
}>;
|
|
190
|
+
operations: Record<string, string>;
|
|
191
|
+
kinds: Record<string, string>;
|
|
192
|
+
actions: Record<string, string>;
|
|
193
|
+
}>>;
|
|
194
|
+
};
|
|
@@ -0,0 +1,342 @@
|
|
|
1
|
+
import { defineEntityType } from '@ismou-workspace/contracts';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
/*
|
|
4
|
+
* `drive.item`: a file or a folder, whoever provides it. A folder is the parent of its items (the
|
|
5
|
+
* same type): sharing a folder shares everything below it, the permissions inheriting by name.
|
|
6
|
+
* An item always belongs to the owner of the folder holding it.
|
|
7
|
+
*/
|
|
8
|
+
export const DRIVE_ITEM_TYPE_ID = 'drive.item';
|
|
9
|
+
export const DRIVE_ITEM_KINDS = ['folder', 'file'];
|
|
10
|
+
export const DriveItemKindSchema = z.enum(DRIVE_ITEM_KINDS);
|
|
11
|
+
/** Characters a name never holds: path separators and control characters. */
|
|
12
|
+
// biome-ignore lint/suspicious/noControlCharactersInRegex: control characters are what it rejects.
|
|
13
|
+
const FORBIDDEN_NAME_CHARACTERS = /[/\\\u0000-\u001f\u007f]/;
|
|
14
|
+
/** The name of a file or a folder, as shown and as downloaded. */
|
|
15
|
+
export const DriveItemNameSchema = z
|
|
16
|
+
.string()
|
|
17
|
+
.trim()
|
|
18
|
+
.min(1)
|
|
19
|
+
.max(255)
|
|
20
|
+
.refine((name) => !FORBIDDEN_NAME_CHARACTERS.test(name), {
|
|
21
|
+
message: 'A name cannot contain slashes or control characters.',
|
|
22
|
+
})
|
|
23
|
+
.refine((name) => name !== '.' && name !== '..', { message: 'This name is reserved.' });
|
|
24
|
+
/** Who created an item (uploaded the file, made the folder), and through which app. */
|
|
25
|
+
export const DriveItemProvenanceSchema = z.object({
|
|
26
|
+
accountId: z.string(),
|
|
27
|
+
appId: z.string().min(1),
|
|
28
|
+
});
|
|
29
|
+
/** An item's data (its id, folder, owner and dates are the envelope's). */
|
|
30
|
+
export const DriveItemSchema = z.object({
|
|
31
|
+
kind: DriveItemKindSchema,
|
|
32
|
+
name: z.string(),
|
|
33
|
+
/** Files only: the type the file was stored with. */
|
|
34
|
+
contentType: z.string().nullable(),
|
|
35
|
+
/** Files only, in bytes. */
|
|
36
|
+
size: z.int().nonnegative().nullable(),
|
|
37
|
+
/** A small picture of the file answers its `thumbnail` operation. */
|
|
38
|
+
hasThumbnail: z.boolean(),
|
|
39
|
+
/** Folders only: it holds at least one folder outside the trash (a tree can unfold it). */
|
|
40
|
+
hasFolders: z.boolean(),
|
|
41
|
+
/**
|
|
42
|
+
* When it was moved to the trash by itself, everything below it with it; `null` otherwise
|
|
43
|
+
* (what lies in a trashed folder is in the trash without being an entry of it).
|
|
44
|
+
*/
|
|
45
|
+
trashedAt: z.iso.datetime({ offset: true }).nullable(),
|
|
46
|
+
createdBy: DriveItemProvenanceSchema,
|
|
47
|
+
});
|
|
48
|
+
/** Whether the item is an entry of the trash (moved there by itself). */
|
|
49
|
+
export const isTrashed = (item) => item.trashedAt !== null;
|
|
50
|
+
/**
|
|
51
|
+
* Creating a folder (a JSON `create`), in a folder (`?parent=`, the folder's owner owning it) or
|
|
52
|
+
* at the top of the owner's drive.
|
|
53
|
+
*/
|
|
54
|
+
export const FolderCreationSchema = z.object({
|
|
55
|
+
name: DriveItemNameSchema,
|
|
56
|
+
/**
|
|
57
|
+
* When an item of that name is already there: `fail` (409, the default), or `reuse` the folder
|
|
58
|
+
* of that name, answered instead (uploading a dropped folder merges it into the one there).
|
|
59
|
+
*/
|
|
60
|
+
ifExists: z.enum(['fail', 'reuse']).default('fail'),
|
|
61
|
+
});
|
|
62
|
+
/**
|
|
63
|
+
* Creating a file is a multipart `create`: the file in this part, named by its file name (only
|
|
64
|
+
* its last segment: browsers name the files of a dropped folder by their path in it). A name
|
|
65
|
+
* already taken in the folder gets a number: "report (2).pdf". At most the core storage's size.
|
|
66
|
+
*/
|
|
67
|
+
export const DRIVE_FILE_PART = 'file';
|
|
68
|
+
/**
|
|
69
|
+
* Changing an item (the body of `update`): renaming it, or moving it to the top of its owner's
|
|
70
|
+
* drive (`root`, the owner only). Moving it into another folder of its owner is `?parent=`.
|
|
71
|
+
* Names are unique in a folder, whatever the case and the accents.
|
|
72
|
+
*/
|
|
73
|
+
export const DriveItemUpdateSchema = z
|
|
74
|
+
.object({
|
|
75
|
+
name: DriveItemNameSchema.optional(),
|
|
76
|
+
root: z.literal(true).optional(),
|
|
77
|
+
})
|
|
78
|
+
.refine(({ name, root }) => name !== undefined || root !== undefined, {
|
|
79
|
+
message: 'Rename it, or move it.',
|
|
80
|
+
});
|
|
81
|
+
/**
|
|
82
|
+
* What a list takes (none: the top of what the call reaches, the owner's top items, or the
|
|
83
|
+
* shared item itself within a share). Every list leaves the trash out, but `trashed`.
|
|
84
|
+
*/
|
|
85
|
+
export const DRIVE_LIST_PARAMS = {
|
|
86
|
+
/**
|
|
87
|
+
* The items of this folder. Not `parent`: the core keeps that name for creations and moves.
|
|
88
|
+
* A folder out of the call's reach lists nothing.
|
|
89
|
+
*/
|
|
90
|
+
folder: 'folder',
|
|
91
|
+
/** Every reachable item whose name holds it, whatever the case and the accents. */
|
|
92
|
+
search: 'search',
|
|
93
|
+
/** `true`: the entries of the trash (within a share, those the acting account trashed). */
|
|
94
|
+
trashed: 'trashed',
|
|
95
|
+
};
|
|
96
|
+
export const DriveItemListParamsSchema = z.object({
|
|
97
|
+
[DRIVE_LIST_PARAMS.folder]: z.string().min(1).optional(),
|
|
98
|
+
[DRIVE_LIST_PARAMS.search]: z.string().trim().min(1).max(100).optional(),
|
|
99
|
+
[DRIVE_LIST_PARAMS.trashed]: z
|
|
100
|
+
.enum(['true', 'false'])
|
|
101
|
+
.optional()
|
|
102
|
+
.transform((trashed) => trashed === 'true'),
|
|
103
|
+
});
|
|
104
|
+
/** Most results a search answers. */
|
|
105
|
+
export const DRIVE_SEARCH_LIMIT = 100;
|
|
106
|
+
/** The item's own operations, and what they take and answer. */
|
|
107
|
+
export const DRIVE_ITEM_OPERATIONS = {
|
|
108
|
+
/**
|
|
109
|
+
* GET → the file's bytes: images and PDFs inline with their type, anything else as an
|
|
110
|
+
* attachment of no particular type. From the trash too.
|
|
111
|
+
*/
|
|
112
|
+
content: 'content',
|
|
113
|
+
/** GET → a small WebP picture of an image file (`hasThumbnail`); 404 without one. */
|
|
114
|
+
thumbnail: 'thumbnail',
|
|
115
|
+
/** GET → `DriveItemAncestors`: where it sits, as far as the call reaches. */
|
|
116
|
+
ancestors: 'ancestors',
|
|
117
|
+
/** POST → 204: moves it to the trash, with everything below it. */
|
|
118
|
+
trash: 'trash',
|
|
119
|
+
/**
|
|
120
|
+
* POST → 204: takes an entry of the trash back where it was (its folder must be out of the
|
|
121
|
+
* trash; a name taken there since gets a number).
|
|
122
|
+
*/
|
|
123
|
+
restore: 'restore',
|
|
124
|
+
/**
|
|
125
|
+
* PUT multipart (`file`: the new bytes; its type kept) → 204: a new version of a file (an
|
|
126
|
+
* editor saving it). Its picture is made again when an app previews its kind.
|
|
127
|
+
*/
|
|
128
|
+
replaceContent: 'replace-content',
|
|
129
|
+
/**
|
|
130
|
+
* POST `DriveItemCopy` → 201 `DriveItemCopyResult`: copies it, with everything live below it,
|
|
131
|
+
* into a folder of the same owner (or the top of the owner's drive), at most
|
|
132
|
+
* `MAX_COPIED_ITEMS` items; the copy takes a free name there. Copying to another owner, or
|
|
133
|
+
* another provider, is a `create` there.
|
|
134
|
+
*/
|
|
135
|
+
copy: 'copy',
|
|
136
|
+
};
|
|
137
|
+
/**
|
|
138
|
+
* The folders above an item the call reaches, from the topmost down to its parent. Within a
|
|
139
|
+
* share, they stop at the shared folder; when the topmost one has a parent, it is out of reach.
|
|
140
|
+
*/
|
|
141
|
+
export const DriveItemAncestorsSchema = z.object({
|
|
142
|
+
ancestors: z.array(z.object({ id: z.string(), name: z.string() })),
|
|
143
|
+
});
|
|
144
|
+
/** Where a copy goes: a folder of the item's owner, or the top of the owner's drive (`null`). */
|
|
145
|
+
export const DriveItemCopySchema = z.object({
|
|
146
|
+
parentId: z.string().min(1).nullable(),
|
|
147
|
+
});
|
|
148
|
+
export const DriveItemCopyResultSchema = z.object({
|
|
149
|
+
/** The copy's id, at the same provider and owner. */
|
|
150
|
+
id: z.string(),
|
|
151
|
+
});
|
|
152
|
+
/** The most items one copy makes (a folder and everything below it). */
|
|
153
|
+
export const MAX_COPIED_ITEMS = 1000;
|
|
154
|
+
/*
|
|
155
|
+
* Kinds and actions: what other apps do with files, whoever provides them. An app supports kinds
|
|
156
|
+
* (`kinds` of its manifest): their icons, and the blank templates new files of a kind start from
|
|
157
|
+
* (the drive's "New" menu). An editor handles `open` (its page shows the file); a previewer
|
|
158
|
+
* handles `preview` (its server answers a picture of the file, the drive's thumbnail of it).
|
|
159
|
+
*/
|
|
160
|
+
/** The kinds of files apps handle apart, and the content types each covers. */
|
|
161
|
+
export const DRIVE_FILE_KINDS = {
|
|
162
|
+
document: [
|
|
163
|
+
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
|
|
164
|
+
'application/msword',
|
|
165
|
+
'application/vnd.oasis.opendocument.text',
|
|
166
|
+
'application/rtf',
|
|
167
|
+
'text/plain',
|
|
168
|
+
],
|
|
169
|
+
spreadsheet: [
|
|
170
|
+
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
|
171
|
+
'application/vnd.ms-excel',
|
|
172
|
+
'application/vnd.oasis.opendocument.spreadsheet',
|
|
173
|
+
'text/csv',
|
|
174
|
+
],
|
|
175
|
+
presentation: [
|
|
176
|
+
'application/vnd.openxmlformats-officedocument.presentationml.presentation',
|
|
177
|
+
'application/vnd.ms-powerpoint',
|
|
178
|
+
'application/vnd.oasis.opendocument.presentation',
|
|
179
|
+
],
|
|
180
|
+
/** A fillable PDF: only ever created (a file of it is a `pdf`). */
|
|
181
|
+
form: ['application/pdf'],
|
|
182
|
+
pdf: ['application/pdf'],
|
|
183
|
+
markdown: ['text/markdown'],
|
|
184
|
+
diagram: [
|
|
185
|
+
'application/vnd.ms-visio.drawing.main+xml',
|
|
186
|
+
'application/vnd.visio',
|
|
187
|
+
'application/vnd.oasis.opendocument.graphics',
|
|
188
|
+
],
|
|
189
|
+
};
|
|
190
|
+
export const DRIVE_FILE_KIND_NAMES = Object.keys(DRIVE_FILE_KINDS);
|
|
191
|
+
/** The actions of `drive.item` other apps handle. */
|
|
192
|
+
export const DRIVE_ITEM_ACTIONS = {
|
|
193
|
+
/** A page showing the file (`read` on it); editing too when the account may write it. */
|
|
194
|
+
open: 'open',
|
|
195
|
+
/** A server answering a picture of the file (`read` on it): PNG, JPEG or WebP. */
|
|
196
|
+
preview: 'preview',
|
|
197
|
+
};
|
|
198
|
+
/**
|
|
199
|
+
* The extensions of each kind's files: browsers often declare none of their types (a `.md` comes
|
|
200
|
+
* as `application/octet-stream`, or as plain text).
|
|
201
|
+
*/
|
|
202
|
+
const DRIVE_FILE_EXTENSIONS = {
|
|
203
|
+
document: ['docx', 'doc', 'odt', 'rtf', 'txt'],
|
|
204
|
+
spreadsheet: ['xlsx', 'xls', 'ods', 'csv'],
|
|
205
|
+
presentation: ['pptx', 'ppt', 'odp'],
|
|
206
|
+
pdf: ['pdf'],
|
|
207
|
+
markdown: ['md', 'markdown'],
|
|
208
|
+
diagram: ['vsdx', 'vsd', 'odg'],
|
|
209
|
+
};
|
|
210
|
+
/**
|
|
211
|
+
* A file's kind, as handlers name it: from its name's extension, the one people see, else its
|
|
212
|
+
* content type; `form` aside (forms are PDFs once made). `undefined` for a folder or a file of no
|
|
213
|
+
* known kind.
|
|
214
|
+
*/
|
|
215
|
+
export function driveItemKind(item) {
|
|
216
|
+
if (item.kind !== 'file')
|
|
217
|
+
return undefined;
|
|
218
|
+
const dot = item.name.lastIndexOf('.');
|
|
219
|
+
const extension = dot > 0 ? item.name.slice(dot + 1).toLowerCase() : '';
|
|
220
|
+
const byExtension = DRIVE_FILE_KIND_NAMES.find((kind) => DRIVE_FILE_EXTENSIONS[kind]?.includes(extension));
|
|
221
|
+
if (byExtension)
|
|
222
|
+
return byExtension;
|
|
223
|
+
const type = item.contentType?.split(';')[0]?.trim().toLowerCase() ?? '';
|
|
224
|
+
return DRIVE_FILE_KIND_NAMES.find((kind) => kind !== 'form' && DRIVE_FILE_KINDS[kind].includes(type));
|
|
225
|
+
}
|
|
226
|
+
export const driveItemType = defineEntityType({
|
|
227
|
+
id: DRIVE_ITEM_TYPE_ID,
|
|
228
|
+
version: 2,
|
|
229
|
+
parent: DRIVE_ITEM_TYPE_ID,
|
|
230
|
+
titleFields: ['name'],
|
|
231
|
+
data: DriveItemSchema,
|
|
232
|
+
// Like most drives: whoever may edit may share, readers may not.
|
|
233
|
+
permissions: { read: null, write: 'read', share: 'write' },
|
|
234
|
+
operations: {
|
|
235
|
+
list: 'read',
|
|
236
|
+
read: 'read',
|
|
237
|
+
create: 'write',
|
|
238
|
+
update: 'write',
|
|
239
|
+
delete: 'write',
|
|
240
|
+
[DRIVE_ITEM_OPERATIONS.content]: 'read',
|
|
241
|
+
[DRIVE_ITEM_OPERATIONS.thumbnail]: 'read',
|
|
242
|
+
[DRIVE_ITEM_OPERATIONS.ancestors]: 'read',
|
|
243
|
+
[DRIVE_ITEM_OPERATIONS.trash]: 'write',
|
|
244
|
+
[DRIVE_ITEM_OPERATIONS.restore]: 'write',
|
|
245
|
+
[DRIVE_ITEM_OPERATIONS.copy]: 'write',
|
|
246
|
+
[DRIVE_ITEM_OPERATIONS.replaceContent]: 'write',
|
|
247
|
+
},
|
|
248
|
+
kinds: DRIVE_FILE_KIND_NAMES,
|
|
249
|
+
actions: {
|
|
250
|
+
[DRIVE_ITEM_ACTIONS.open]: { permission: 'read', target: 'page' },
|
|
251
|
+
[DRIVE_ITEM_ACTIONS.preview]: { permission: 'read', target: 'server' },
|
|
252
|
+
},
|
|
253
|
+
messages: {
|
|
254
|
+
en: {
|
|
255
|
+
name: 'File or folder',
|
|
256
|
+
plural: 'Files and folders',
|
|
257
|
+
permissions: {
|
|
258
|
+
read: {
|
|
259
|
+
label: 'View files',
|
|
260
|
+
description: 'See files and folders, open and download them.',
|
|
261
|
+
},
|
|
262
|
+
write: {
|
|
263
|
+
label: 'Edit files',
|
|
264
|
+
description: 'Upload, rename, move, copy, trash and delete files and folders.',
|
|
265
|
+
},
|
|
266
|
+
share: { label: 'Share files', description: 'Share files and folders with other people.' },
|
|
267
|
+
},
|
|
268
|
+
operations: {
|
|
269
|
+
list: 'List files and folders',
|
|
270
|
+
read: 'View a file or a folder',
|
|
271
|
+
create: 'Upload files and create folders',
|
|
272
|
+
update: 'Rename and move',
|
|
273
|
+
delete: 'Delete for good',
|
|
274
|
+
content: 'Open or download a file',
|
|
275
|
+
thumbnail: 'Show a picture of a file',
|
|
276
|
+
ancestors: 'Show where an item sits',
|
|
277
|
+
trash: 'Move to the trash',
|
|
278
|
+
restore: 'Restore from the trash',
|
|
279
|
+
copy: 'Make a copy',
|
|
280
|
+
'replace-content': 'Save a new version',
|
|
281
|
+
},
|
|
282
|
+
kinds: {
|
|
283
|
+
document: 'Document',
|
|
284
|
+
spreadsheet: 'Spreadsheet',
|
|
285
|
+
presentation: 'Presentation',
|
|
286
|
+
form: 'Form',
|
|
287
|
+
pdf: 'PDF',
|
|
288
|
+
markdown: 'Markdown',
|
|
289
|
+
diagram: 'Diagram',
|
|
290
|
+
},
|
|
291
|
+
actions: {
|
|
292
|
+
open: 'Open with {app}',
|
|
293
|
+
preview: 'Preview with {app}',
|
|
294
|
+
},
|
|
295
|
+
},
|
|
296
|
+
fr: {
|
|
297
|
+
name: 'Fichier ou dossier',
|
|
298
|
+
plural: 'Fichiers et dossiers',
|
|
299
|
+
permissions: {
|
|
300
|
+
read: {
|
|
301
|
+
label: 'Voir les fichiers',
|
|
302
|
+
description: 'Voir les fichiers et les dossiers, les ouvrir et les télécharger.',
|
|
303
|
+
},
|
|
304
|
+
write: {
|
|
305
|
+
label: 'Modifier les fichiers',
|
|
306
|
+
description: 'Importer, renommer, déplacer, copier, mettre à la corbeille et supprimer des fichiers et des dossiers.',
|
|
307
|
+
},
|
|
308
|
+
share: {
|
|
309
|
+
label: 'Partager les fichiers',
|
|
310
|
+
description: "Partager des fichiers et des dossiers avec d'autres personnes.",
|
|
311
|
+
},
|
|
312
|
+
},
|
|
313
|
+
operations: {
|
|
314
|
+
list: 'Lister les fichiers et les dossiers',
|
|
315
|
+
read: 'Voir un fichier ou un dossier',
|
|
316
|
+
create: 'Importer des fichiers et créer des dossiers',
|
|
317
|
+
update: 'Renommer et déplacer',
|
|
318
|
+
delete: 'Supprimer définitivement',
|
|
319
|
+
content: 'Ouvrir ou télécharger un fichier',
|
|
320
|
+
thumbnail: "Afficher l'aperçu d'un fichier",
|
|
321
|
+
ancestors: "Afficher l'emplacement d'un élément",
|
|
322
|
+
trash: 'Mettre à la corbeille',
|
|
323
|
+
restore: 'Restaurer depuis la corbeille',
|
|
324
|
+
copy: 'Faire une copie',
|
|
325
|
+
'replace-content': 'Enregistrer une nouvelle version',
|
|
326
|
+
},
|
|
327
|
+
kinds: {
|
|
328
|
+
document: 'Document',
|
|
329
|
+
spreadsheet: 'Classeur',
|
|
330
|
+
presentation: 'Présentation',
|
|
331
|
+
form: 'Formulaire',
|
|
332
|
+
pdf: 'PDF',
|
|
333
|
+
markdown: 'Markdown',
|
|
334
|
+
diagram: 'Diagramme',
|
|
335
|
+
},
|
|
336
|
+
actions: {
|
|
337
|
+
open: 'Ouvrir avec {app}',
|
|
338
|
+
preview: 'Aperçu avec {app}',
|
|
339
|
+
},
|
|
340
|
+
},
|
|
341
|
+
},
|
|
342
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,38 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ismou-workspace/drive-contracts",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "The drive.item entity type of Ismou's Drive app (files and folders), and the DTOs of its API.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"sideEffects": false,
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"default": "./dist/index.js"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
"dependencies": {
|
|
14
|
+
"@ismou-workspace/contracts": "^0.1.1",
|
|
15
|
+
"zod": "4.6.7"
|
|
16
|
+
},
|
|
17
|
+
"devDependencies": {
|
|
18
|
+
"@ismou-workspace/tsconfig": "^0.1.1",
|
|
19
|
+
"typescript": "7.0.2",
|
|
20
|
+
"vitest": "5.1.2"
|
|
21
|
+
},
|
|
22
|
+
"repository": {
|
|
23
|
+
"type": "git",
|
|
24
|
+
"url": "git+https://github.com/ismou-workspace/drive.git",
|
|
25
|
+
"directory": "contracts"
|
|
26
|
+
},
|
|
27
|
+
"files": [
|
|
28
|
+
"dist"
|
|
29
|
+
],
|
|
30
|
+
"publishConfig": {
|
|
31
|
+
"access": "public"
|
|
32
|
+
},
|
|
33
|
+
"scripts": {
|
|
34
|
+
"build": "tsc --project tsconfig.build.json",
|
|
35
|
+
"typecheck": "tsc --project tsconfig.json",
|
|
36
|
+
"test": "vitest run"
|
|
37
|
+
}
|
|
6
38
|
}
|