appwrite-utils-cli 0.0.32 → 0.0.34

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.
@@ -1,83 +1,83 @@
1
- import {
2
- OpenAPIRegistry,
3
- OpenApiGeneratorV3,
4
- OpenApiGeneratorV31,
5
- } from "@asteasolutions/zod-to-openapi";
6
- import {
7
- attributeSchema,
8
- CollectionSchema,
9
- type AppwriteConfig,
10
- type Attribute,
11
- type Collection,
12
- type CollectionCreate,
13
- } from "appwrite-utils";
14
- import { z } from "zod";
15
- import { writeFileSync } from "fs";
16
-
17
- const registry = new OpenAPIRegistry();
18
-
19
- export const generateOpenApi = async (config: AppwriteConfig) => {
20
- if (!config.collections) {
21
- return;
22
- }
23
- for (const collection of config.collections) {
24
- // Transform and register each attribute schema
25
- const attributeSchemas = collection.attributes.map((attribute) => {
26
- return transformTypeToOpenApi(attributeSchema, attribute.description);
27
- });
28
-
29
- // Create and register the collection schema with descriptions
30
- const updatedCollectionSchema = CollectionSchema.extend({
31
- // @ts-ignore
32
- attributes: attributeSchemas,
33
- }).openapi(collection.description ?? "No description");
34
-
35
- // Register the updated collection schema under the collection name
36
- registry.register(collection.name, updatedCollectionSchema);
37
- }
38
-
39
- // Convert the registry to OpenAPI JSON
40
- const generator = new OpenApiGeneratorV31(registry.definitions);
41
- const openApiSpec = generator.generateComponents();
42
-
43
- // Output the OpenAPI spec to a file
44
- writeFileSync(
45
- "./appwrite/openapi/openapi.json",
46
- JSON.stringify(openApiSpec, null, 2)
47
- );
48
- };
49
-
50
- export function transformTypeToOpenApi<T extends z.ZodTypeAny>(
51
- schema: T,
52
- description?: string | Record<string, any> | null | undefined
53
- ): T {
54
- // Check if description is an object (OpenAPI properties) or a string
55
- let updatedSchema: z.infer<T>;
56
- if (!description) {
57
- return schema;
58
- }
59
- if (typeof description === "string") {
60
- updatedSchema = schema.openapi(description);
61
- } else if (typeof description === "object") {
62
- updatedSchema = schema.openapi(description);
63
- } else {
64
- updatedSchema = schema;
65
- }
66
-
67
- // Check and transform attributes if they exist
68
- if ((schema as any)._def && (schema as any)._def.shape) {
69
- const shape = (schema as any)._def.shape();
70
- for (const key in shape) {
71
- const attributeDesc = shape[key].description;
72
- if (attributeDesc) {
73
- if (typeof attributeDesc === "string") {
74
- shape[key] = shape[key].openapi(attributeDesc);
75
- } else if (typeof attributeDesc === "object") {
76
- shape[key] = shape[key].openapi(attributeDesc);
77
- }
78
- }
79
- }
80
- }
81
-
82
- return updatedSchema;
83
- }
1
+ import {
2
+ OpenAPIRegistry,
3
+ OpenApiGeneratorV3,
4
+ OpenApiGeneratorV31,
5
+ } from "@asteasolutions/zod-to-openapi";
6
+ import {
7
+ attributeSchema,
8
+ CollectionSchema,
9
+ type AppwriteConfig,
10
+ type Attribute,
11
+ type Collection,
12
+ type CollectionCreate,
13
+ } from "appwrite-utils";
14
+ import { z } from "zod";
15
+ import { writeFileSync } from "fs";
16
+
17
+ const registry = new OpenAPIRegistry();
18
+
19
+ export const generateOpenApi = async (config: AppwriteConfig) => {
20
+ if (!config.collections) {
21
+ return;
22
+ }
23
+ for (const collection of config.collections) {
24
+ // Transform and register each attribute schema
25
+ const attributeSchemas = collection.attributes.map((attribute) => {
26
+ return transformTypeToOpenApi(attributeSchema, attribute.description);
27
+ });
28
+
29
+ // Create and register the collection schema with descriptions
30
+ const updatedCollectionSchema = CollectionSchema.extend({
31
+ // @ts-ignore
32
+ attributes: attributeSchemas,
33
+ }).openapi(collection.description ?? "No description");
34
+
35
+ // Register the updated collection schema under the collection name
36
+ registry.register(collection.name, updatedCollectionSchema);
37
+ }
38
+
39
+ // Convert the registry to OpenAPI JSON
40
+ const generator = new OpenApiGeneratorV31(registry.definitions);
41
+ const openApiSpec = generator.generateComponents();
42
+
43
+ // Output the OpenAPI spec to a file
44
+ writeFileSync(
45
+ "./appwrite/openapi/openapi.json",
46
+ JSON.stringify(openApiSpec, null, 2)
47
+ );
48
+ };
49
+
50
+ export function transformTypeToOpenApi<T extends z.ZodTypeAny>(
51
+ schema: T,
52
+ description?: string | Record<string, any> | null | undefined
53
+ ): T {
54
+ // Check if description is an object (OpenAPI properties) or a string
55
+ let updatedSchema: z.infer<T>;
56
+ if (!description) {
57
+ return schema;
58
+ }
59
+ if (typeof description === "string") {
60
+ updatedSchema = schema.openapi(description);
61
+ } else if (typeof description === "object") {
62
+ updatedSchema = schema.openapi(description);
63
+ } else {
64
+ updatedSchema = schema;
65
+ }
66
+
67
+ // Check and transform attributes if they exist
68
+ if ((schema as any)._def && (schema as any)._def.shape) {
69
+ const shape = (schema as any)._def.shape();
70
+ for (const key in shape) {
71
+ const attributeDesc = shape[key].description;
72
+ if (attributeDesc) {
73
+ if (typeof attributeDesc === "string") {
74
+ shape[key] = shape[key].openapi(attributeDesc);
75
+ } else if (typeof attributeDesc === "object") {
76
+ shape[key] = shape[key].openapi(attributeDesc);
77
+ }
78
+ }
79
+ }
80
+ }
81
+
82
+ return updatedSchema;
83
+ }