@scalar/workspace-store 0.2.0 → 0.3.1

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.
@@ -4,14 +4,12 @@ import { createMagicProxy } from './helpers/proxy'
4
4
  import { isObject } from '@/helpers/general'
5
5
  import { getValueByPath } from '@/helpers/json-path-utils'
6
6
  import { bundle } from '@scalar/openapi-parser'
7
- import { readFiles } from '@scalar/openapi-parser/utils/bundle/plugins/read-files'
8
7
  import { fetchUrls } from '@scalar/openapi-parser/utils/bundle/plugins/fetch-urls'
9
8
 
10
9
  type WorkspaceDocumentMetaInput = { meta?: WorkspaceDocumentMeta; name: string }
11
10
  type WorkspaceDocumentInput =
12
11
  | ({ document: Record<string, unknown> } & WorkspaceDocumentMetaInput)
13
12
  | ({ url: string } & WorkspaceDocumentMetaInput)
14
- | ({ path: string } & WorkspaceDocumentMetaInput)
15
13
 
16
14
  /**
17
15
  * Resolves a workspace document from various input sources (URL, local file, or direct document object).
@@ -42,10 +40,6 @@ async function loadDocument(workspaceDocument: WorkspaceDocumentInput) {
42
40
  return fetchUrls().exec(workspaceDocument.url)
43
41
  }
44
42
 
45
- if ('path' in workspaceDocument) {
46
- return readFiles().exec(workspaceDocument.path)
47
- }
48
-
49
43
  return {
50
44
  ok: true as const,
51
45
  data: workspaceDocument.document,
@@ -60,45 +54,16 @@ async function loadDocument(workspaceDocument: WorkspaceDocumentInput) {
60
54
  * @param workspaceProps.meta - Optional metadata for the workspace
61
55
  * @param workspaceProps.documents - Optional record of documents to initialize the workspace with
62
56
  * @returns An object containing methods and getters for managing the workspace
57
+ * @deprecated Use `createWorkspaceStore` instead.
63
58
  */
64
- export async function createWorkspaceStore(workspaceProps?: {
59
+ export function createWorkspaceStoreSync(workspaceProps?: {
65
60
  meta?: WorkspaceMeta
66
- documents?: WorkspaceDocumentInput[]
67
61
  }) {
68
62
  // Create a reactive workspace object with proxied documents
69
63
  // Each document is wrapped in a proxy to enable reactive updates and reference resolution
70
64
  const workspace = reactive({
71
65
  ...workspaceProps?.meta,
72
- documents: (
73
- await Promise.all(
74
- (workspaceProps?.documents ?? []).map<
75
- Promise<{ name: string; meta?: WorkspaceDocumentMeta; document: Record<string, unknown> }>
76
- >(async (data) => {
77
- const resolved = await loadDocument(data)
78
-
79
- if (!resolved.ok) {
80
- console.error(`Can not load the document '${data.name}'`)
81
- return {
82
- name: data.name,
83
- meta: data.meta,
84
- document: {},
85
- }
86
- }
87
-
88
- return {
89
- name: data.name,
90
- meta: data.meta,
91
- document: isObject(resolved.data) ? (resolved.data as Record<string, unknown>) : {},
92
- }
93
- }),
94
- )
95
- ).reduce<Record<string, Record<string, unknown>>>((acc, { name, meta, document }) => {
96
- /**
97
- * We wrap each document in the magic proxy to enable auto-resolving of references
98
- */
99
- acc[name] = createMagicProxy({ ...document, ...meta })
100
- return acc
101
- }, {}),
66
+ documents: {},
102
67
  /**
103
68
  * Returns the currently active document from the workspace.
104
69
  * The active document is determined by the 'x-scalar-active-document' metadata field,
@@ -164,7 +129,7 @@ export async function createWorkspaceStore(workspaceProps?: {
164
129
  const currentDocument =
165
130
  workspace.documents[
166
131
  name === 'active'
167
- ? (workspace['x-scalar-active-document'] ?? workspaceProps?.documents?.[0].name ?? '')
132
+ ? (workspace['x-scalar-active-document'] ?? Object.keys(workspace.documents)[0] ?? '')
168
133
  : name
169
134
  ]
170
135
 
@@ -202,7 +167,7 @@ export async function createWorkspaceStore(workspaceProps?: {
202
167
  return bundle(target, {
203
168
  root: activeDocument,
204
169
  treeShake: false,
205
- plugins: [fetchUrls(), readFiles()],
170
+ plugins: [fetchUrls()],
206
171
  urlMap: false,
207
172
  hooks: {
208
173
  onResolveStart: (node) => {
@@ -240,6 +205,9 @@ export async function createWorkspaceStore(workspaceProps?: {
240
205
 
241
206
  if (!resolve.ok || !isObject(resolve.data)) {
242
207
  console.error(`Can not load the document '${name}'`)
208
+ workspace.documents[name] = {
209
+ ...meta,
210
+ }
243
211
  return
244
212
  }
245
213
 
@@ -247,3 +215,40 @@ export async function createWorkspaceStore(workspaceProps?: {
247
215
  },
248
216
  }
249
217
  }
218
+
219
+ /**
220
+ * Creates a reactive workspace store that manages documents and their metadata.
221
+ * The store provides functionality for accessing, updating, and resolving document references.
222
+ *
223
+ * @param workspaceProps - Configuration object for the workspace
224
+ * @param workspaceProps.meta - Optional metadata for the workspace
225
+ * @param workspaceProps.documents - Optional record of documents to initialize the workspace with
226
+ * @returns An object containing methods and getters for managing the workspace
227
+ * @example
228
+ * // Create a workspace store with metadata and documents
229
+ * const store = await createWorkspaceStore({
230
+ * meta: {
231
+ * name: 'My Workspace',
232
+ * description: 'A workspace for my API'
233
+ * },
234
+ * documents: [
235
+ * {
236
+ * name: 'petstore',
237
+ * document: {
238
+ * openapi: '3.0.0',
239
+ * info: { title: 'Petstore API' }
240
+ * }
241
+ * }
242
+ * ]
243
+ * })
244
+ */
245
+ export async function createWorkspaceStore(workspaceProps?: {
246
+ meta?: WorkspaceMeta
247
+ documents?: WorkspaceDocumentInput[]
248
+ }) {
249
+ const store = createWorkspaceStoreSync({ meta: workspaceProps?.meta })
250
+
251
+ await Promise.all(workspaceProps?.documents?.map((it) => store.addDocument(it)) ?? [])
252
+
253
+ return store
254
+ }
package/src/schemas.ts ADDED
@@ -0,0 +1,6 @@
1
+ export {
2
+ WorkspaceSchema,
3
+ type Workspace,
4
+ WorkspaceDocumentSchema,
5
+ type WorkspaceDocument,
6
+ } from './schemas/server-workspace'
@@ -5,7 +5,7 @@ import {
5
5
  externalizeComponentReferences,
6
6
  externalizePathReferences,
7
7
  filterHttpMethodsOnly,
8
- } from './create-server-workspace-store'
8
+ } from './server'
9
9
  import fs from 'node:fs/promises'
10
10
  import { cwd } from 'node:process'
11
11
  import { allFilesMatch } from '../test/helpers'
@@ -1 +0,0 @@
1
- {"version":3,"file":"create-server-workspace-store.d.ts","sourceRoot":"","sources":["../src/create-server-workspace-store.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,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