document-drive 0.0.1 → 0.0.3

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "document-drive",
3
- "version": "0.0.1",
3
+ "version": "0.0.3",
4
4
  "license": "AGPL-3.0-only",
5
5
  "type": "module",
6
6
  "module": "./src/index.ts",
@@ -56,9 +56,9 @@ export class DocumentDriveServer implements IDocumentDriveServer {
56
56
 
57
57
  async createDocument(driveId: string, input: CreateDocumentInput) {
58
58
  const documentModel = this._getDocumentModel(input.documentType);
59
- const document = documentModel.utils.createDocument({
60
- // state: input.initialState, TODO add initial state
61
- });
59
+
60
+ // TODO validate input.document is of documentType
61
+ const document = input.document ?? documentModel.utils.createDocument();
62
62
 
63
63
  return this.storage.saveDocument(driveId, input.id, document);
64
64
  }
@@ -12,7 +12,7 @@ export type DriveInput = Omit<
12
12
  export type CreateDocumentInput = {
13
13
  id: string;
14
14
  documentType: string;
15
- // initialState?: ExtendedState<S>; TODO add support for initial state
15
+ document?: Document;
16
16
  };
17
17
 
18
18
  export interface SortOptions {
@@ -1,5 +1,6 @@
1
1
  import { DocumentDriveDocument } from 'document-model-libs/document-drive';
2
2
  import { Document } from 'document-model/document';
3
+ import type { Dirent } from 'fs';
3
4
  import { existsSync, mkdirSync } from 'fs';
4
5
  import fs from 'fs/promises';
5
6
 
@@ -14,6 +15,13 @@ function ensureDir(dir: string) {
14
15
  }
15
16
  }
16
17
 
18
+ type FSError = {
19
+ errno: number;
20
+ code: string;
21
+ syscall: string;
22
+ path: string;
23
+ };
24
+
17
25
  export class FilesystemStorage implements IDriveStorage {
18
26
  private basePath: string;
19
27
  private drivesPath: string;
@@ -37,9 +45,17 @@ export class FilesystemStorage implements IDriveStorage {
37
45
  }
38
46
 
39
47
  async getDocuments(drive: string) {
40
- const files = await fs.readdir(path.join(this.basePath, drive), {
41
- withFileTypes: true
42
- });
48
+ let files: Dirent[] = [];
49
+ try {
50
+ files = await fs.readdir(path.join(this.basePath, drive), {
51
+ withFileTypes: true
52
+ });
53
+ } catch (error) {
54
+ // if folder is not found then drive has no documents
55
+ if ((error as FSError).code !== 'ENOENT') {
56
+ throw error;
57
+ }
58
+ }
43
59
  const documents: string[] = [];
44
60
  for (const file of files.filter(file => file.isFile())) {
45
61
  try {