@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 1,611 lines 49.4 KB
Raw
1 import { assert, describe, it } from "@effect/vitest"
2 import { Schema } from "effect"
3 import { Multipart } from "effect/http"
4 import {
5 HttpApi,
6 HttpApiEndpoint,
7 HttpApiError,
8 HttpApiGroup,
9 HttpApiMiddleware,
10 HttpApiSchema,
11 OpenApi
12 } from "effect/http-api"
13
14 describe("OpenAPI spec", () => {
15 describe("api", () => {
16 it("annotate", () => {
17 const Api = HttpApi.make("api")
18 .annotate(OpenApi.Title, "title")
19 .annotate(OpenApi.Version, "version")
20 .annotate(OpenApi.Description, "description")
21 .annotate(OpenApi.License, { name: "license" })
22 .annotate(OpenApi.Summary, "summary")
23 .annotate(OpenApi.Servers, [{ url: "https://example.com" }])
24 const spec = OpenApi.fromApi(Api)
25 assert.deepStrictEqual(spec.info.title, "title")
26 assert.deepStrictEqual(spec.info.version, "version")
27 assert.deepStrictEqual(spec.info.description, "description")
28 assert.deepStrictEqual(spec.info.license, { name: "license" })
29 assert.deepStrictEqual(spec.info.summary, "summary")
30 assert.deepStrictEqual(spec.servers, [{ url: "https://example.com" }])
31 })
32
33 it("annotateMerge", () => {
34 const Api = HttpApi.make("api").annotateMerge(OpenApi.annotations({
35 title: "title",
36 version: "version",
37 description: "description",
38 license: { name: "license" },
39 summary: "summary",
40 servers: [{ url: "https://example.com" }]
41 }))
42 const spec = OpenApi.fromApi(Api)
43 assert.deepStrictEqual(spec.info.title, "title")
44 assert.deepStrictEqual(spec.info.version, "version")
45 assert.deepStrictEqual(spec.info.description, "description")
46 assert.deepStrictEqual(spec.info.license, { name: "license" })
47 assert.deepStrictEqual(spec.info.summary, "summary")
48 assert.deepStrictEqual(spec.servers, [{ url: "https://example.com" }])
49 })
50
51 it("AdditionalSchemas", () => {
52 const Api = HttpApi.make("api")
53 .annotate(HttpApi.AdditionalSchemas, [Schema.String.annotate({ identifier: "A" })])
54 const spec = OpenApi.fromApi(Api)
55 assert.deepStrictEqual(spec.components.schemas, {
56 A: {
57 "type": "string"
58 }
59 })
60 })
61
62 it("Override", () => {
63 const Api = HttpApi.make("api")
64 .annotate(OpenApi.Override, {
65 tags: [{ name: "A" }]
66 })
67 const spec = OpenApi.fromApi(Api)
68 assert.deepStrictEqual(spec.tags, [{ name: "A" }])
69 })
70
71 it("Transform", () => {
72 const Api = HttpApi.make("api")
73 .annotate(OpenApi.Transform, (spec) => ({
74 ...spec,
75 tags: [{ name: "A" }]
76 }))
77 const spec = OpenApi.fromApi(Api)
78 assert.deepStrictEqual(spec.tags, [{ name: "A" }])
79 })
80 })
81
82 describe("group", () => {
83 it("annotations", () => {
84 const Api = HttpApi.make("api")
85 .add(
86 HttpApiGroup.make("group")
87 // Add a description for the group
88 .annotate(OpenApi.Description, "my description")
89 // Provide external documentation links
90 .annotate(OpenApi.ExternalDocs, {
91 url: "http://example.com",
92 description: "example"
93 })
94 // Override parts of the final output
95 .annotate(OpenApi.Override, { name: "my name" })
96 // Transform the final specification for this group
97 .annotate(OpenApi.Transform, (spec) => ({
98 ...spec,
99 name: spec.name + "-transformed"
100 }))
101 )
102 .add(
103 HttpApiGroup.make("excluded")
104 // Exclude the group from the final specification
105 .annotate(OpenApi.Exclude, true)
106 )
107 const spec = OpenApi.fromApi(Api)
108 assert.deepStrictEqual(spec, {
109 "openapi": "3.1.0",
110 "info": {
111 "title": "Api",
112 "version": "0.0.1"
113 },
114 "paths": {},
115 "components": {
116 "schemas": {},
117 "securitySchemes": {}
118 },
119 "security": [],
120 "tags": [
121 {
122 "name": "my name-transformed",
123 "description": "my description",
124 "externalDocs": {
125 "url": "http://example.com",
126 "description": "example"
127 }
128 }
129 ]
130 })
131 })
132
133 it("top level group", () => {
134 const Api = HttpApi.make("api").add(
135 // Mark the group as top-level
136 HttpApiGroup.make("group", { topLevel: true }).add(
137 HttpApiEndpoint.get("get", "/", {
138 success: Schema.String
139 })
140 )
141 )
142 const spec = OpenApi.fromApi(Api)
143 assert.deepStrictEqual(spec.paths, {
144 "/": {
145 "get": {
146 "tags": [
147 "group"
148 ],
149 "operationId": "get",
150 "parameters": [],
151 "security": [],
152 "responses": {
153 "200": {
154 "description": "Success",
155 "content": {
156 "application/json": {
157 "schema": {
158 "type": "string"
159 }
160 }
161 }
162 }
163 }
164 }
165 }
166 })
167 })
168 })
169
170 describe("endpoint", () => {
171 describe("annotations", () => {
172 it("endpoint annotations", () => {
173 const Api = HttpApi.make("api").add(
174 HttpApiGroup.make("group")
175 .add(
176 HttpApiEndpoint.get("get", "/", {
177 success: Schema.String
178 })
179 // Add a description
180 .annotate(OpenApi.Description, "my description")
181 // Provide a summary
182 .annotate(OpenApi.Summary, "my summary")
183 // Mark the endpoint as deprecated
184 .annotate(OpenApi.Deprecated, true)
185 // Provide external documentation
186 .annotate(OpenApi.ExternalDocs, {
187 url: "http://example.com",
188 description: "example"
189 })
190 )
191 .add(
192 HttpApiEndpoint.get("excluded", "/excluded", {
193 success: Schema.String
194 })
195 // Exclude this endpoint from the final specification
196 .annotate(OpenApi.Exclude, true)
197 )
198 )
199 const spec = OpenApi.fromApi(Api)
200 assert.deepStrictEqual(spec, {
201 "openapi": "3.1.0",
202 "info": {
203 "title": "Api",
204 "version": "0.0.1"
205 },
206 "paths": {
207 "/": {
208 "get": {
209 "tags": [
210 "group"
211 ],
212 "operationId": "group.get",
213 "parameters": [],
214 "security": [],
215 "responses": {
216 "200": {
217 "description": "Success",
218 "content": {
219 "application/json": {
220 "schema": {
221 "type": "string"
222 }
223 }
224 }
225 }
226 },
227 "description": "my description",
228 "summary": "my summary",
229 "deprecated": true,
230 "externalDocs": {
231 "url": "http://example.com",
232 "description": "example"
233 }
234 }
235 }
236 },
237 "components": {
238 "schemas": {},
239 "securitySchemes": {}
240 },
241 "security": [],
242 "tags": [
243 {
244 "name": "group"
245 }
246 ]
247 })
248 })
249
250 it("success description", () => {
251 const User = Schema.Struct({
252 id: Schema.Finite,
253 name: Schema.String
254 }).annotate({ identifier: "User" })
255
256 const Api = HttpApi.make("api").add(
257 HttpApiGroup.make("group").add(
258 HttpApiEndpoint.get("getUsers", "/users", {
259 success: Schema.Array(User).annotate({
260 description: "Returns an array of users"
261 })
262 })
263 )
264 )
265 const spec = OpenApi.fromApi(Api)
266 assert.deepStrictEqual(spec.paths, {
267 "/users": {
268 "get": {
269 "tags": [
270 "group"
271 ],
272 "operationId": "group.getUsers",
273 "parameters": [],
274 "security": [],
275 "responses": {
276 "200": {
277 "description": "Returns an array of users",
278 "content": {
279 "application/json": {
280 "schema": {
281 "type": "array",
282 "items": {
283 "$ref": "#/components/schemas/User"
284 },
285 "description": "Returns an array of users"
286 }
287 }
288 }
289 }
290 }
291 }
292 }
293 })
294 assert.deepStrictEqual(spec.components.schemas, {
295 User: {
296 "type": "object",
297 "properties": {
298 "id": {
299 "type": "number"
300 },
301 "name": {
302 "type": "string"
303 }
304 },
305 "required": [
306 "id",
307 "name"
308 ],
309 "additionalProperties": false
310 }
311 })
312 })
313 })
314
315 it("catch all path", () => {
316 const Api = HttpApi.make("api")
317 .add(
318 HttpApiGroup.make("group")
319 .add(
320 HttpApiEndpoint.get("a", "*", {
321 success: Schema.String
322 })
323 )
324 )
325 const spec = OpenApi.fromApi(Api)
326 assert.deepStrictEqual(spec.paths["*"].get?.responses, {
327 "200": {
328 description: "Success",
329 content: {
330 "application/json": {
331 schema: {
332 "type": "string"
333 }
334 }
335 }
336 }
337 })
338 })
339
340 it("middleware error", () => {
341 class M extends HttpApiMiddleware.Service<M>()("Http/Logger", {
342 error: Schema.String
343 .pipe(
344 HttpApiSchema.status(405),
345 HttpApiSchema.asText()
346 )
347 }) {}
348
349 const Api = HttpApi.make("api")
350 .add(
351 HttpApiGroup.make("group")
352 .add(
353 HttpApiEndpoint.get("a", "/a", {
354 success: Schema.Finite
355 })
356 ).middleware(M)
357 )
358 const spec = OpenApi.fromApi(Api)
359 assert.deepStrictEqual(spec.paths["/a"].get?.responses, {
360 "200": {
361 description: "Success",
362 content: {
363 "application/json": {
364 schema: {
365 "type": "number"
366 }
367 }
368 }
369 },
370 "405": {
371 description: "Error",
372 content: {
373 "text/plain": {
374 schema: {
375 "type": "string"
376 }
377 }
378 }
379 }
380 })
381 })
382
383 describe("params option", () => {
384 it("GET", () => {
385 const Api = HttpApi.make("api")
386 .add(
387 HttpApiGroup.make("group")
388 .add(
389 HttpApiEndpoint.get("a", "/a/:id", {
390 params: {
391 id: Schema.FiniteFromString
392 }
393 })
394 )
395 )
396 const spec = OpenApi.fromApi(Api)
397 assert.deepStrictEqual(spec.paths["/a/{id}"].get?.parameters, [
398 {
399 name: "id",
400 in: "path",
401 schema: {
402 "type": "string"
403 },
404 required: true
405 }
406 ])
407 })
408 })
409
410 describe("payload option", () => {
411 describe("GET", () => {
412 it("query parameters", () => {
413 const Api = HttpApi.make("api")
414 .add(
415 HttpApiGroup.make("group")
416 .add(
417 HttpApiEndpoint.get("a", "/a", {
418 payload: {
419 required: Schema.FiniteFromString,
420 optionalKey: Schema.optionalKey(Schema.FiniteFromString),
421 optional: Schema.optional(Schema.FiniteFromString)
422 }
423 })
424 )
425 )
426 const spec = OpenApi.fromApi(Api)
427 assert.deepStrictEqual(spec.paths["/a"].get?.parameters, [
428 {
429 name: "required",
430 in: "query",
431 schema: {
432 "type": "string"
433 },
434 required: true
435 },
436 {
437 name: "optionalKey",
438 in: "query",
439 schema: {
440 "type": "string"
441 },
442 required: false
443 },
444 {
445 name: "optional",
446 in: "query",
447 schema: {
448 "type": "string"
449 },
450 required: false
451 }
452 ])
453 })
454 })
455
456 describe("POST", () => {
457 it("NoContent", () => {
458 const Api = HttpApi.make("api")
459 .add(
460 HttpApiGroup.make("group")
461 .add(HttpApiEndpoint.post("a", "/a", { payload: HttpApiSchema.NoContent }))
462 )
463 const spec = OpenApi.fromApi(Api)
464 assert.deepStrictEqual(spec.paths["/a"].post?.requestBody, undefined)
465 })
466
467 it("NoContent + payload", () => {
468 const Api = HttpApi.make("api")
469 .add(
470 HttpApiGroup.make("group")
471 .add(HttpApiEndpoint.post("a", "/a", { payload: [HttpApiSchema.NoContent, Schema.String] }))
472 )
473 const spec = OpenApi.fromApi(Api)
474 assert.deepStrictEqual(spec.paths["/a"].post?.requestBody?.content, {
475 "application/json": {
476 schema: {
477 "type": "string"
478 }
479 }
480 })
481 })
482
483 it("Json (default)", () => {
484 const Api = HttpApi.make("api")
485 .add(
486 HttpApiGroup.make("group")
487 .add(
488 HttpApiEndpoint.post("a", "/a", {
489 payload: Schema.String
490 })
491 )
492 )
493 const spec = OpenApi.fromApi(Api)
494 assert.deepStrictEqual(spec.paths["/a"].post?.requestBody?.content, {
495 "application/json": {
496 schema: {
497 "type": "string"
498 }
499 }
500 })
501 })
502
503 describe("encodings", () => {
504 it("asMultipart", () => {
505 const Api = HttpApi.make("api")
506 .add(
507 HttpApiGroup.make("group")
508 .add(
509 HttpApiEndpoint.post("a", "/a", {
510 payload: Schema.String.pipe(HttpApiSchema.asMultipart())
511 })
512 )
513 )
514 const spec = OpenApi.fromApi(Api)
515 assert.deepStrictEqual(spec.paths["/a"].post?.requestBody?.content, {
516 "multipart/form-data": {
517 schema: {
518 "type": "string"
519 }
520 }
521 })
522 })
523
524 it("asMultipartStream", () => {
525 const Api = HttpApi.make("api")
526 .add(
527 HttpApiGroup.make("group")
528 .add(
529 HttpApiEndpoint.post("a", "/a", {
530 payload: Schema.String.pipe(HttpApiSchema.asMultipartStream())
531 })
532 )
533 )
534 const spec = OpenApi.fromApi(Api)
535 assert.deepStrictEqual(spec.paths["/a"].post?.requestBody?.content, {
536 "multipart/form-data": {
537 schema: {
538 "type": "string"
539 }
540 }
541 })
542 })
543
544 it("asJson + contentType", () => {
545 const Api = HttpApi.make("api")
546 .add(
547 HttpApiGroup.make("group")
548 .add(
549 HttpApiEndpoint.post("a", "/a", {
550 payload: Schema.String.pipe(
551 HttpApiSchema.asJson({ contentType: "application/problem+json" })
552 )
553 })
554 )
555 )
556 const spec = OpenApi.fromApi(Api)
557 assert.deepStrictEqual(spec.paths["/a"].post?.requestBody?.content, {
558 "application/problem+json": {
559 schema: {
560 "type": "string"
561 }
562 }
563 })
564 })
565
566 it("asText", () => {
567 const Api = HttpApi.make("api")
568 .add(
569 HttpApiGroup.make("group")
570 .add(
571 HttpApiEndpoint.post("a", "/a", {
572 payload: Schema.String.pipe(HttpApiSchema.asText())
573 })
574 )
575 )
576 const spec = OpenApi.fromApi(Api)
577 assert.deepStrictEqual(spec.paths["/a"].post?.requestBody?.content, {
578 "text/plain": {
579 schema: {
580 "type": "string"
581 }
582 }
583 })
584 })
585
586 it("asFormUrlEncoded", () => {
587 const Api = HttpApi.make("api")
588 .add(
589 HttpApiGroup.make("group")
590 .add(
591 HttpApiEndpoint.post("a", "/a", {
592 payload: Schema.Struct({ a: Schema.String }).pipe(
593 HttpApiSchema.asFormUrlEncoded()
594 )
595 })
596 )
597 )
598 const spec = OpenApi.fromApi(Api)
599 assert.deepStrictEqual(spec.paths["/a"].post?.requestBody?.content, {
600 "application/x-www-form-urlencoded": {
601 schema: {
602 "type": "object",
603 "properties": {
604 "a": {
605 "type": "string"
606 }
607 },
608 "required": ["a"],
609 "additionalProperties": false
610 }
611 }
612 })
613 })
614
615 it("asUint8Array", () => {
616 const Api = HttpApi.make("api")
617 .add(
618 HttpApiGroup.make("group")
619 .add(
620 HttpApiEndpoint.post("a", "/a", {
621 payload: Schema.Uint8Array.pipe(HttpApiSchema.asUint8Array())
622 })
623 )
624 )
625 const spec = OpenApi.fromApi(Api)
626 assert.deepStrictEqual(spec.paths["/a"].post?.requestBody?.content, {
627 "application/octet-stream": {
628 schema: {
629 "type": "string",
630 "format": "binary"
631 }
632 }
633 })
634 })
635
636 it("array of schemas with different encodings", () => {
637 const Api = HttpApi.make("api")
638 .add(
639 HttpApiGroup.make("group")
640 .add(
641 HttpApiEndpoint.post("a", "/a", {
642 payload: [
643 Schema.Struct({ a: Schema.String }), // application/json
644 Schema.String.pipe(HttpApiSchema.asText()), // text/plain
645 Schema.Uint8Array.pipe(HttpApiSchema.asUint8Array()) // application/octet-stream
646 ]
647 })
648 )
649 )
650 const spec = OpenApi.fromApi(Api)
651 assert.deepStrictEqual(spec.paths["/a"].post?.requestBody?.content, {
652 "application/json": {
653 schema: {
654 "type": "object",
655 "properties": {
656 "a": {
657 "type": "string"
658 }
659 },
660 "required": ["a"],
661 "additionalProperties": false
662 }
663 },
664 "text/plain": {
665 schema: {
666 "type": "string"
667 }
668 },
669 "application/octet-stream": {
670 schema: {
671 "type": "string",
672 "format": "binary"
673 }
674 }
675 })
676 })
677
678 it("asMultipart with FilesSchema", () => {
679 const Api = HttpApi.make("api")
680 .add(
681 HttpApiGroup.make("group")
682 .add(
683 HttpApiEndpoint.post("a", "/a", {
684 payload: Multipart.FilesSchema.pipe(HttpApiSchema.asMultipart())
685 })
686 )
687 )
688 const spec = OpenApi.fromApi(Api)
689 assert.deepStrictEqual(spec.paths["/a"].post?.requestBody?.content, {
690 "multipart/form-data": {
691 schema: {
692 type: "array",
693 items: {
694 type: "string",
695 format: "binary"
696 }
697 }
698 }
699 })
700 })
701
702 it("asMultipart with SingleFileSchema", () => {
703 const Api = HttpApi.make("api")
704 .add(
705 HttpApiGroup.make("group")
706 .add(
707 HttpApiEndpoint.post("a", "/a", {
708 payload: Multipart.SingleFileSchema.pipe(HttpApiSchema.asMultipart())
709 })
710 )
711 )
712 const spec = OpenApi.fromApi(Api)
713 assert.deepStrictEqual(spec.paths["/a"].post?.requestBody?.content, {
714 "multipart/form-data": {
715 schema: {
716 type: "string",
717 format: "binary"
718 }
719 }
720 })
721 })
722
723 it("asMultipart with SingleFileSchema in Struct", () => {
724 const Api = HttpApi.make("api")
725 .add(
726 HttpApiGroup.make("group")
727 .add(
728 HttpApiEndpoint.post("a", "/a", {
729 payload: Schema.Struct({
730 file: Multipart.SingleFileSchema
731 }).pipe(HttpApiSchema.asMultipart())
732 })
733 )
734 )
735 const spec = OpenApi.fromApi(Api)
736 assert.deepStrictEqual(spec.paths["/a"].post?.requestBody?.content, {
737 "multipart/form-data": {
738 schema: {
739 type: "object",
740 properties: {
741 file: {
742 type: "string",
743 format: "binary"
744 }
745 },
746 required: ["file"],
747 additionalProperties: false
748 }
749 }
750 })
751 })
752
753 it("FilesSchema in non-multipart payload should not be converted to binary", () => {
754 const Api = HttpApi.make("api")
755 .add(
756 HttpApiGroup.make("group")
757 .add(
758 HttpApiEndpoint.post("a", "/a", {
759 payload: Schema.Struct({
760 files: Multipart.FilesSchema
761 })
762 })
763 )
764 )
765 const spec = OpenApi.fromApi(Api)
766 assert.deepStrictEqual(spec.paths["/a"].post?.requestBody?.content, {
767 "application/json": {
768 schema: {
769 type: "object",
770 properties: {
771 files: {
772 type: "array",
773 items: {
774 type: "object",
775 properties: {
776 key: { "type": "string" },
777 name: { "type": "string" },
778 contentType: { type: "string", contentEncoding: "binary" },
779 path: { "type": "string" }
780 },
781 required: ["key", "name", "contentType", "path"],
782 additionalProperties: false
783 }
784 }
785 },
786 required: ["files"],
787 additionalProperties: false
788 }
789 }
790 })
791 })
792 })
793 })
794 })
795
796 describe("success option", () => {
797 it("no content (default)", () => {
798 const Api = HttpApi.make("api")
799 .add(
800 HttpApiGroup.make("group")
801 .add(HttpApiEndpoint.get("a", "/a"))
802 )
803 const spec = OpenApi.fromApi(Api)
804 assert.deepStrictEqual(spec.paths["/a"].get?.responses["204"], {
805 description: "<No Content>"
806 })
807 })
808
809 it("schema", () => {
810 const Api = HttpApi.make("api")
811 .add(
812 HttpApiGroup.make("group")
813 .add(HttpApiEndpoint.get("a", "/a", {
814 success: Schema.String
815 }))
816 )
817 const spec = OpenApi.fromApi(Api)
818 assert.deepStrictEqual(spec.paths["/a"].get?.responses["200"].content, {
819 "application/json": {
820 schema: {
821 "type": "string"
822 }
823 }
824 })
825 })
826
827 describe("NoContent", () => {
828 it("makeNoContent(204)", () => {
829 const Api = HttpApi.make("api")
830 .add(
831 HttpApiGroup.make("group")
832 .add(HttpApiEndpoint.get("a", "/a", {
833 success: HttpApiSchema.Empty(204)
834 }))
835 )
836 const spec = OpenApi.fromApi(Api)
837 assert.deepStrictEqual(spec.paths["/a"].get?.responses["204"], {
838 description: "<No Content>"
839 })
840 })
841
842 it("NoContent", () => {
843 const Api = HttpApi.make("api")
844 .add(
845 HttpApiGroup.make("group")
846 .add(HttpApiEndpoint.get("a", "/a", {
847 success: HttpApiSchema.NoContent
848 }))
849 )
850 const spec = OpenApi.fromApi(Api)
851 assert.deepStrictEqual(spec.paths["/a"].get?.responses["204"], {
852 description: "<No Content>"
853 })
854 })
855
856 it("Created", () => {
857 const Api = HttpApi.make("api")
858 .add(
859 HttpApiGroup.make("group")
860 .add(HttpApiEndpoint.get("a", "/a", {
861 success: HttpApiSchema.Created
862 }))
863 )
864 const spec = OpenApi.fromApi(Api)
865 assert.deepStrictEqual(spec.paths["/a"].get?.responses["201"], {
866 description: "<No Content>"
867 })
868 })
869
870 it("status(\"Created\")", () => {
871 const Api = HttpApi.make("api")
872 .add(
873 HttpApiGroup.make("group")
874 .add(HttpApiEndpoint.get("a", "/a", {
875 success: Schema.Void.pipe(HttpApiSchema.status("Created"))
876 }))
877 )
878 const spec = OpenApi.fromApi(Api)
879 assert.deepStrictEqual(spec.paths["/a"].get?.responses["201"], {
880 description: "<No Content>"
881 })
882 })
883
884 it("Accepted", () => {
885 const Api = HttpApi.make("api")
886 .add(
887 HttpApiGroup.make("group")
888 .add(HttpApiEndpoint.get("a", "/a", {
889 success: HttpApiSchema.Accepted
890 }))
891 )
892 const spec = OpenApi.fromApi(Api)
893 assert.deepStrictEqual(spec.paths["/a"].get?.responses["202"], {
894 description: "<No Content>"
895 })
896 })
897 })
898
899 describe("encodings", () => {
900 it("asJson + contentType", () => {
901 const Api = HttpApi.make("api")
902 .add(
903 HttpApiGroup.make("group")
904 .add(
905 HttpApiEndpoint.get("a", "/a", {
906 success: Schema.String.pipe(
907 HttpApiSchema.asJson({ contentType: "application/problem+json" })
908 )
909 })
910 )
911 )
912 const spec = OpenApi.fromApi(Api)
913 assert.deepStrictEqual(spec.paths["/a"].get?.responses["200"].content, {
914 "application/problem+json": {
915 schema: {
916 "type": "string"
917 }
918 }
919 })
920 })
921
922 it("asText", () => {
923 const Api = HttpApi.make("api")
924 .add(
925 HttpApiGroup.make("group")
926 .add(
927 HttpApiEndpoint.get("a", "/a", {
928 success: Schema.String.pipe(HttpApiSchema.asText())
929 })
930 )
931 )
932 const spec = OpenApi.fromApi(Api)
933 assert.deepStrictEqual(spec.paths["/a"].get?.responses["200"].content, {
934 "text/plain": {
935 schema: {
936 "type": "string"
937 }
938 }
939 })
940 })
941
942 it("asFormUrlEncoded", () => {
943 const Api = HttpApi.make("api")
944 .add(
945 HttpApiGroup.make("group")
946 .add(
947 HttpApiEndpoint.get("a", "/a", {
948 success: Schema.Struct({ a: Schema.String }).pipe(
949 HttpApiSchema.asFormUrlEncoded()
950 )
951 })
952 )
953 )
954 const spec = OpenApi.fromApi(Api)
955 assert.deepStrictEqual(spec.paths["/a"].get?.responses["200"].content, {
956 "application/x-www-form-urlencoded": {
957 schema: {
958 "type": "object",
959 "properties": {
960 "a": {
961 "type": "string"
962 }
963 },
964 "required": ["a"],
965 "additionalProperties": false
966 }
967 }
968 })
969 })
970
971 it("asUint8Array", () => {
972 const Api = HttpApi.make("api")
973 .add(
974 HttpApiGroup.make("group")
975 .add(
976 HttpApiEndpoint.get("a", "/a", {
977 success: Schema.Uint8Array.pipe(HttpApiSchema.asUint8Array())
978 })
979 )
980 )
981 const spec = OpenApi.fromApi(Api)
982 assert.deepStrictEqual(spec.paths["/a"].get?.responses["200"].content, {
983 "application/octet-stream": {
984 schema: {
985 "type": "string",
986 "format": "binary"
987 }
988 }
989 })
990 })
991
992 it("StreamSse", () => {
993 const Api = HttpApi.make("api")
994 .add(
995 HttpApiGroup.make("group")
996 .add(
997 HttpApiEndpoint.get("a", "/a", {
998 success: HttpApiSchema.StreamSse({
999 events: Schema.Struct({
1000 event: Schema.Literal("message"),
1001 data: Schema.String
1002 }),
1003 error: Schema.Struct({
1004 message: Schema.String
1005 })
1006 })
1007 })
1008 )
1009 )
1010 const spec = OpenApi.fromApi(Api)
1011 const mediaType = spec.paths["/a"].get?.responses["200"].content?.["text/event-stream"]
1012 const stream = mediaType?.["x-effect-stream"]
1013 assert.strictEqual(stream?.encoding, "sse")
1014 if (stream?.encoding !== "sse") {
1015 assert.fail("expected SSE stream metadata")
1016 }
1017 assert.strictEqual(stream.failureEvent, "effect/http-api/stream/failure")
1018 assert.strictEqual(typeof stream.causeSchema, "object")
1019 assert.strictEqual(typeof stream.errorSchema, "object")
1020 })
1021
1022 it("StreamSse with annotated status", () => {
1023 const Api = HttpApi.make("api")
1024 .add(
1025 HttpApiGroup.make("group")
1026 .add(
1027 HttpApiEndpoint.get("a", "/a", {
1028 success: HttpApiSchema.status(202)(
1029 HttpApiSchema.StreamSse({
1030 events: Schema.Struct({
1031 event: Schema.Literal("message"),
1032 data: Schema.String
1033 }),
1034 error: Schema.Struct({
1035 message: Schema.String
1036 })
1037 })
1038 )
1039 })
1040 )
1041 )
1042 const spec = OpenApi.fromApi(Api)
1043 assert.strictEqual(spec.paths["/a"].get?.responses["200"], undefined)
1044 assert.strictEqual(
1045 spec.paths["/a"].get?.responses["202"].content?.["text/event-stream"]?.["x-effect-stream"]?.encoding,
1046 "sse"
1047 )
1048 })
1049
1050 it("StreamUint8Array", () => {
1051 const Api = HttpApi.make("api")
1052 .add(
1053 HttpApiGroup.make("group")
1054 .add(
1055 HttpApiEndpoint.get("a", "/a", {
1056 success: HttpApiSchema.StreamUint8Array()
1057 })
1058 )
1059 )
1060 const spec = OpenApi.fromApi(Api)
1061 assert.deepStrictEqual(spec.paths["/a"].get?.responses["200"].content, {
1062 "application/octet-stream": {
1063 schema: {
1064 "type": "string",
1065 "format": "binary"
1066 },
1067 "x-effect-stream": {
1068 encoding: "uint8array"
1069 }
1070 }
1071 })
1072 })
1073
1074 it("Upgrade", () => {
1075 const Api = HttpApi.make("api")
1076 .add(
1077 HttpApiGroup.make("group")
1078 .add(
1079 HttpApiEndpoint.post("a", "/a", {
1080 payload: Schema.Struct({ tty: Schema.Boolean }),
1081 success: HttpApiSchema.Upgrade({
1082 protocol: "tcp",
1083 headers: { "x-stream": Schema.Literals(["raw", "multiplexed"]) }
1084 })
1085 })
1086 )
1087 )
1088 const spec = OpenApi.fromApi(Api)
1089 assert.deepStrictEqual(spec.paths["/a"].post?.responses["101"], {
1090 description: "Switching Protocols",
1091 headers: {
1092 connection: { schema: { type: "string", enum: ["Upgrade"] }, required: true },
1093 upgrade: { schema: { type: "string", enum: ["tcp"] }, required: true },
1094 "x-stream": { schema: { type: "string", enum: ["raw", "multiplexed"] }, required: true }
1095 }
1096 })
1097 assert.deepStrictEqual(Object.keys(spec.paths["/a"].post?.requestBody?.content ?? {}), ["application/json"])
1098 assert.strictEqual(spec.paths["/a"].post?.responses["200"], undefined)
1099 })
1100
1101 it("Upgrade with an ordinary success", () => {
1102 const Api = HttpApi.make("api")
1103 .add(
1104 HttpApiGroup.make("group")
1105 .add(
1106 HttpApiEndpoint.post("a", "/a", {
1107 success: [HttpApiSchema.Upgrade({ protocol: "tcp" }), HttpApiSchema.Empty(200)]
1108 })
1109 )
1110 )
1111 const spec = OpenApi.fromApi(Api)
1112 assert.deepStrictEqual(Object.keys(spec.paths["/a"].post?.responses ?? {}).sort(), ["101", "200"])
1113 })
1114
1115 it("StreamUint8Array with annotated status", () => {
1116 const Api = HttpApi.make("api")
1117 .add(
1118 HttpApiGroup.make("group")
1119 .add(
1120 HttpApiEndpoint.get("a", "/a", {
1121 success: HttpApiSchema.status(206)(HttpApiSchema.StreamUint8Array())
1122 })
1123 )
1124 )
1125 const spec = OpenApi.fromApi(Api)
1126 assert.strictEqual(spec.paths["/a"].get?.responses["200"], undefined)
1127 assert.deepStrictEqual(spec.paths["/a"].get?.responses["206"].content, {
1128 "application/octet-stream": {
1129 schema: {
1130 "type": "string",
1131 "format": "binary"
1132 },
1133 "x-effect-stream": {
1134 encoding: "uint8array"
1135 }
1136 }
1137 })
1138 })
1139
1140 it("union with top level encoding", () => {
1141 const Api = HttpApi.make("api")
1142 .add(
1143 HttpApiGroup.make("group")
1144 .add(
1145 HttpApiEndpoint.get("a", "/a", {
1146 success: Schema.Union([
1147 Schema.Struct({ a: Schema.String }),
1148 Schema.Struct({ b: Schema.String })
1149 ]).pipe(HttpApiSchema.asFormUrlEncoded())
1150 })
1151 )
1152 )
1153 const spec = OpenApi.fromApi(Api)
1154 assert.deepStrictEqual(spec.paths["/a"].get?.responses["200"].content, {
1155 "application/x-www-form-urlencoded": {
1156 schema: {
1157 "anyOf": [
1158 {
1159 "type": "object",
1160 "properties": {
1161 "a": {
1162 "type": "string"
1163 }
1164 },
1165 "required": ["a"],
1166 "additionalProperties": false
1167 },
1168 {
1169 "type": "object",
1170 "properties": {
1171 "b": {
1172 "type": "string"
1173 }
1174 },
1175 "required": ["b"],
1176 "additionalProperties": false
1177 }
1178 ]
1179 }
1180 }
1181 })
1182 })
1183
1184 it("asJson + astText", () => {
1185 const Api = HttpApi.make("api")
1186 .add(
1187 HttpApiGroup.make("group")
1188 .add(
1189 HttpApiEndpoint.get("a", "/a", {
1190 success: [
1191 Schema.String,
1192 Schema.String.pipe(
1193 HttpApiSchema.asText()
1194 )
1195 ]
1196 })
1197 )
1198 )
1199 const spec = OpenApi.fromApi(Api)
1200 assert.deepStrictEqual(spec.paths["/a"].get?.responses["200"].content, {
1201 "application/json": {
1202 "schema": {
1203 "type": "string"
1204 }
1205 },
1206 "text/plain": {
1207 "schema": {
1208 "type": "string"
1209 }
1210 }
1211 })
1212 })
1213 })
1214 })
1215
1216 describe("error option", () => {
1217 it("should deduplicate the descriptions", () => {
1218 class Group extends HttpApiGroup.make("users")
1219 .add(HttpApiEndpoint.post("a", "/a", {
1220 error: HttpApiError.BadRequestNoContent
1221 }))
1222 {}
1223
1224 class Api extends HttpApi.make("api").add(Group) {}
1225 const spec = OpenApi.fromApi(Api)
1226 assert.deepStrictEqual(spec.paths["/a"].post?.responses["400"], {
1227 description: "BadRequest"
1228 })
1229 assert.deepStrictEqual(spec.components.schemas, {})
1230 })
1231
1232 describe("1 endopoint", () => {
1233 it("no identifier annotation", () => {
1234 class Group extends HttpApiGroup.make("users")
1235 .add(HttpApiEndpoint.post("a", "/a", {
1236 payload: Schema.String,
1237 success: Schema.String,
1238 error: Schema.String
1239 }))
1240 {}
1241
1242 class Api extends HttpApi.make("api").add(Group) {}
1243 const spec = OpenApi.fromApi(Api)
1244 assert.deepStrictEqual(spec.paths["/a"].post?.responses, {
1245 "200": {
1246 description: "Success",
1247 content: {
1248 "application/json": {
1249 schema: {
1250 "type": "string"
1251 }
1252 }
1253 }
1254 },
1255 "500": {
1256 description: "Error",
1257 content: {
1258 "application/json": {
1259 schema: {
1260 "type": "string"
1261 }
1262 }
1263 }
1264 }
1265 })
1266 assert.deepStrictEqual(spec.components.schemas, {})
1267 })
1268
1269 it("identifier annotation", () => {
1270 class Group extends HttpApiGroup.make("users")
1271 .add(HttpApiEndpoint.post("a", "/a", {
1272 payload: Schema.String,
1273 success: Schema.String,
1274 error: Schema.String.annotate({ identifier: "id" })
1275 }))
1276 {}
1277
1278 class Api extends HttpApi.make("api").add(Group) {}
1279 const spec = OpenApi.fromApi(Api)
1280 assert.deepStrictEqual(spec.paths["/a"].post?.responses, {
1281 "200": {
1282 description: "Success",
1283 content: {
1284 "application/json": {
1285 schema: {
1286 "type": "string"
1287 }
1288 }
1289 }
1290 },
1291 "500": {
1292 description: "id",
1293 content: {
1294 "application/json": {
1295 schema: {
1296 "$ref": "#/components/schemas/id"
1297 }
1298 }
1299 }
1300 }
1301 })
1302 assert.deepStrictEqual(spec.components.schemas, {
1303 id: {
1304 "type": "string"
1305 }
1306 })
1307 })
1308
1309 it("httpApiStatus annotation", () => {
1310 class Group extends HttpApiGroup.make("users")
1311 .add(HttpApiEndpoint.post("a", "/a", {
1312 payload: Schema.String,
1313 success: Schema.String,
1314 error: Schema.String.annotate({ httpApiStatus: 400 })
1315 }))
1316 {}
1317
1318 class Api extends HttpApi.make("api").add(Group) {}
1319 const spec = OpenApi.fromApi(Api)
1320 assert.deepStrictEqual(spec.paths["/a"].post?.responses, {
1321 "200": {
1322 description: "Success",
1323 content: {
1324 "application/json": {
1325 schema: {
1326 "type": "string"
1327 }
1328 }
1329 }
1330 },
1331 "400": {
1332 description: "Error",
1333 content: {
1334 "application/json": {
1335 schema: {
1336 "type": "string"
1337 }
1338 }
1339 }
1340 }
1341 })
1342 assert.deepStrictEqual(spec.components.schemas, {})
1343 })
1344 })
1345
1346 describe("2 endopoints", () => {
1347 it("no identifier annotation", () => {
1348 const E = Schema.String
1349 class Group extends HttpApiGroup.make("users")
1350 .add(HttpApiEndpoint.post("a", "/a", {
1351 payload: Schema.String,
1352 success: Schema.String,
1353 error: E
1354 }))
1355 .add(HttpApiEndpoint.post("b", "/b", {
1356 payload: Schema.String,
1357 success: Schema.String,
1358 error: E
1359 }))
1360 {}
1361
1362 class Api extends HttpApi.make("api").add(Group) {}
1363 const spec = OpenApi.fromApi(Api)
1364 assert.deepStrictEqual(spec.paths["/a"].post?.responses, {
1365 "200": {
1366 description: "Success",
1367 content: {
1368 "application/json": {
1369 schema: {
1370 "type": "string"
1371 }
1372 }
1373 }
1374 },
1375 "500": {
1376 description: "Error",
1377 content: {
1378 "application/json": {
1379 schema: {
1380 "type": "string"
1381 }
1382 }
1383 }
1384 }
1385 })
1386 assert.deepStrictEqual(spec.paths["/b"].post?.responses, {
1387 "200": {
1388 description: "Success",
1389 content: {
1390 "application/json": {
1391 schema: {
1392 "type": "string"
1393 }
1394 }
1395 }
1396 },
1397 "500": {
1398 description: "Error",
1399 content: {
1400 "application/json": {
1401 schema: {
1402 "type": "string"
1403 }
1404 }
1405 }
1406 }
1407 })
1408 assert.deepStrictEqual(spec.components.schemas, {})
1409 })
1410
1411 it("identifier annotation", () => {
1412 const E = Schema.String.annotate({ identifier: "id" })
1413 class Group extends HttpApiGroup.make("users")
1414 .add(HttpApiEndpoint.post("a", "/a", {
1415 payload: Schema.String,
1416 success: Schema.String,
1417 error: E
1418 }))
1419 .add(HttpApiEndpoint.post("b", "/b", {
1420 payload: Schema.String,
1421 success: Schema.String,
1422 error: E
1423 }))
1424 {}
1425
1426 class Api extends HttpApi.make("api").add(Group) {}
1427 const spec = OpenApi.fromApi(Api)
1428 assert.deepStrictEqual(spec.paths["/a"].post?.responses, {
1429 "200": {
1430 description: "Success",
1431 content: {
1432 "application/json": {
1433 schema: {
1434 "type": "string"
1435 }
1436 }
1437 }
1438 },
1439 "500": {
1440 description: "id",
1441 content: {
1442 "application/json": {
1443 schema: {
1444 "$ref": "#/components/schemas/id"
1445 }
1446 }
1447 }
1448 }
1449 })
1450 assert.deepStrictEqual(spec.paths["/b"].post?.responses, {
1451 "200": {
1452 description: "Success",
1453 content: {
1454 "application/json": {
1455 schema: {
1456 "type": "string"
1457 }
1458 }
1459 }
1460 },
1461 "500": {
1462 description: "id",
1463 content: {
1464 "application/json": {
1465 schema: {
1466 "$ref": "#/components/schemas/id"
1467 }
1468 }
1469 }
1470 }
1471 })
1472 assert.deepStrictEqual(spec.components.schemas, {
1473 id: {
1474 "type": "string"
1475 }
1476 })
1477 })
1478
1479 it("httpApiStatus annotation", () => {
1480 const E = Schema.String.annotate({ httpApiStatus: 400 })
1481 class Group extends HttpApiGroup.make("users")
1482 .add(HttpApiEndpoint.post("a", "/a", {
1483 payload: Schema.String,
1484 success: Schema.String,
1485 error: E
1486 }))
1487 .add(HttpApiEndpoint.post("b", "/b", {
1488 payload: Schema.String,
1489 success: Schema.String,
1490 error: E
1491 }))
1492 {}
1493
1494 class Api extends HttpApi.make("api").add(Group) {}
1495 const spec = OpenApi.fromApi(Api)
1496 assert.deepStrictEqual(spec.paths["/a"].post?.responses, {
1497 "200": {
1498 description: "Success",
1499 content: {
1500 "application/json": {
1501 schema: {
1502 "type": "string"
1503 }
1504 }
1505 }
1506 },
1507 "400": {
1508 description: "Error",
1509 content: {
1510 "application/json": {
1511 schema: {
1512 "type": "string"
1513 }
1514 }
1515 }
1516 }
1517 })
1518 assert.deepStrictEqual(spec.paths["/b"].post?.responses, {
1519 "200": {
1520 description: "Success",
1521 content: {
1522 "application/json": {
1523 schema: {
1524 "type": "string"
1525 }
1526 }
1527 }
1528 },
1529 "400": {
1530 description: "Error",
1531 content: {
1532 "application/json": {
1533 schema: {
1534 "type": "string"
1535 }
1536 }
1537 }
1538 }
1539 })
1540 assert.deepStrictEqual(spec.components.schemas, {})
1541 })
1542 })
1543
1544 describe("No Content", () => {
1545 it("makeNoContent(400)", () => {
1546 const Api = HttpApi.make("api")
1547 .add(
1548 HttpApiGroup.make("group")
1549 .add(
1550 HttpApiEndpoint.get("a", "/a", {
1551 error: HttpApiSchema.Empty(400)
1552 })
1553 )
1554 )
1555 const spec = OpenApi.fromApi(Api)
1556 assert.deepStrictEqual(spec.paths["/a"].get?.responses["400"], {
1557 description: "<No Content>"
1558 })
1559 })
1560
1561 it("Empty(401)", () => {
1562 const Api = HttpApi.make("api")
1563 .add(
1564 HttpApiGroup.make("group")
1565 .add(
1566 HttpApiEndpoint.get("a", "/a", {
1567 error: HttpApiSchema.Empty(401)
1568 })
1569 )
1570 )
1571 const spec = OpenApi.fromApi(Api)
1572 assert.deepStrictEqual(spec.paths["/a"].get?.responses["401"], {
1573 description: "<No Content>"
1574 })
1575 })
1576
1577 it("UnauthorizedNoContent", () => {
1578 const Api = HttpApi.make("api")
1579 .add(
1580 HttpApiGroup.make("group")
1581 .add(
1582 HttpApiEndpoint.get("a", "/a", {
1583 error: HttpApiError.UnauthorizedNoContent
1584 })
1585 )
1586 )
1587 const spec = OpenApi.fromApi(Api)
1588 assert.deepStrictEqual(spec.paths["/a"].get?.responses["401"], {
1589 description: "Unauthorized"
1590 })
1591 })
1592
1593 it("BadRequestNoContent", () => {
1594 const Api = HttpApi.make("api")
1595 .add(
1596 HttpApiGroup.make("group")
1597 .add(
1598 HttpApiEndpoint.get("a", "/a", {
1599 error: HttpApiError.BadRequestNoContent
1600 })
1601 )
1602 )
1603 const spec = OpenApi.fromApi(Api)
1604 assert.deepStrictEqual(spec.paths["/a"].get?.responses["400"], {
1605 description: "BadRequest"
1606 })
1607 })
1608 })
1609 })
1610 })
1611 })