11import * as E from 'fp-ts/lib/Either' ;
22import assert from 'node:assert' ;
33import test from 'node:test' ;
4+ import type { NestedDirectoryJSON } from 'memfs' ;
45
5- import { parseSource , parseApiSpec , Project , type Route } from '../src' ;
6+ import { TestProject } from './testProject' ;
7+ import { parseApiSpec , type Route } from '../src' ;
68
79async function testCase (
810 description : string ,
9- src : string ,
11+ files : NestedDirectoryJSON ,
12+ entryPoint : string ,
1013 expected : Record < string , Route [ ] > ,
1114 expectedErrors : string [ ] = [ ] ,
1215) {
1316 test ( description , async ( ) => {
14- const sourceFile = await parseSource ( './index.ts' , src ) ;
17+ const project = new TestProject ( files ) ;
18+
19+ await project . parseEntryPoint ( entryPoint ) ;
20+ const sourceFile = project . get ( entryPoint ) ;
21+ if ( sourceFile === undefined ) {
22+ throw new Error ( `could not find source file ${ entryPoint } ` ) ;
23+ }
1524
1625 const actual : Record < string , Route [ ] > = { } ;
1726 const errors : string [ ] = [ ] ;
@@ -32,7 +41,7 @@ async function testCase(
3241 if ( arg . expression . type !== 'ObjectExpression' ) {
3342 continue ;
3443 }
35- const result = parseApiSpec ( new Project ( ) , sourceFile , arg . expression ) ;
44+ const result = parseApiSpec ( project , sourceFile , arg . expression ) ;
3645 if ( E . isLeft ( result ) ) {
3746 errors . push ( result . left ) ;
3847 } else {
@@ -46,24 +55,57 @@ async function testCase(
4655 } ) ;
4756}
4857
49- const SIMPLE = `
50- import * as t from 'io-ts';
51- import * as h from '@api-ts/io-ts-http';
52- export const test = h.apiSpec({
53- 'api.test': {
54- get: h.httpRoute({
58+ const SIMPLE = {
59+ '/index.ts' : `
60+ import * as t from 'io-ts';
61+ import * as h from '@api-ts/io-ts-http';
62+ export const test = h.apiSpec({
63+ 'api.test': {
64+ get: h.httpRoute({
65+ path: '/test',
66+ method: 'GET',
67+ request: h.httpRequest({}),
68+ response: {
69+ 200: t.string,
70+ },
71+ })
72+ }
73+ });` ,
74+ } ;
75+
76+ testCase ( 'simple api spec' , SIMPLE , '/index.ts' , {
77+ test : [
78+ {
79+ path : '/test' ,
80+ method : 'GET' ,
81+ parameters : [ ] ,
82+ response : { 200 : { type : 'primitive' , value : 'string' } } ,
83+ } ,
84+ ] ,
85+ } ) ;
86+
87+ const ROUTE_REF = {
88+ '/index.ts' : `
89+ import * as t from 'io-ts';
90+ import * as h from '@api-ts/io-ts-http';
91+
92+ const testRoute = h.httpRoute({
5593 path: '/test',
5694 method: 'GET',
5795 request: h.httpRequest({}),
5896 response: {
5997 200: t.string,
6098 },
61- })
62- }
63- });
64- ` ;
99+ });
65100
66- testCase ( 'simple api spec' , SIMPLE , {
101+ export const test = h.apiSpec({
102+ 'api.test': {
103+ get: testRoute,
104+ }
105+ });` ,
106+ } ;
107+
108+ testCase ( 'const route reference' , ROUTE_REF , '/index.ts' , {
67109 test : [
68110 {
69111 path : '/test' ,
@@ -74,27 +116,28 @@ testCase('simple api spec', SIMPLE, {
74116 ] ,
75117} ) ;
76118
77- const ROUTE_REF = `
78- import * as t from 'io-ts';
79- import * as h from '@api-ts/io-ts-http';
80-
81- const testRoute = h.httpRoute({
82- path: '/test',
83- method: 'GET',
84- request: h.httpRequest({}),
85- response: {
86- 200: t.string,
87- },
88- });
119+ const ACTION_REF = {
120+ '/index.ts' : `
121+ import * as t from 'io-ts';
122+ import * as h from '@api-ts/io-ts-http';
89123
90- export const test = h.apiSpec({
91- 'api.test': {
92- get: testRoute,
93- }
94- });
95- ` ;
124+ const testAction = {
125+ get: h.httpRoute({
126+ path: '/test',
127+ method: 'GET',
128+ request: h.httpRequest({}),
129+ response: {
130+ 200: t.string,
131+ },
132+ }),
133+ };
134+
135+ export const test = h.apiSpec({
136+ 'api.test': testAction,
137+ });` ,
138+ } ;
96139
97- testCase ( 'const route reference' , ROUTE_REF , {
140+ testCase ( 'const action reference' , ACTION_REF , '/index.ts' , {
98141 test : [
99142 {
100143 path : '/test' ,
@@ -105,27 +148,68 @@ testCase('const route reference', ROUTE_REF, {
105148 ] ,
106149} ) ;
107150
108- const ACTION_REF = `
109- import * as t from 'io-ts';
110- import * as h from '@api-ts/io-ts-http';
111-
112- const testAction = {
113- get: h.httpRoute({
114- path: '/test',
115- method: 'GET',
116- request: h.httpRequest({}),
117- response: {
118- 200: t.string,
119- },
120- }),
151+ const SPREAD = {
152+ '/index.ts' : `
153+ import * as h from '@api-ts/io-ts-http';
154+
155+ import { Ref } from './ref';
156+
157+ export const test = h.apiSpec({
158+ ...Ref,
159+ });` ,
160+ '/ref.ts' : `
161+ import * as t from 'io-ts';
162+ import * as h from '@api-ts/io-ts-http';
163+ export const Ref = {
164+ 'api.test': {
165+ get: h.httpRoute({
166+ path: '/test',
167+ method: 'GET',
168+ request: h.httpRequest({}),
169+ response: {
170+ 200: t.string,
171+ },
172+ })
173+ }
174+ };
175+ ` ,
121176} ;
122177
123- export const test = h.apiSpec({
124- 'api.test': testAction,
178+ testCase ( 'spread api spec' , SPREAD , '/index.ts' , {
179+ test : [
180+ {
181+ path : '/test' ,
182+ method : 'GET' ,
183+ parameters : [ ] ,
184+ response : { 200 : { type : 'primitive' , value : 'string' } } ,
185+ } ,
186+ ] ,
125187} ) ;
126- ` ;
127188
128- testCase ( 'const action reference' , ACTION_REF , {
189+ const COMPUTED_PROPERTY = {
190+ '/index.ts' : `
191+ import * as t from 'io-ts';
192+ import * as h from '@api-ts/io-ts-http';
193+
194+ function test(): 'api.test' {
195+ return 'api.test';
196+ }
197+
198+ export const test = h.apiSpec({
199+ [test()]: {
200+ get: h.httpRoute({
201+ path: '/test',
202+ method: 'GET',
203+ request: h.httpRequest({}),
204+ response: {
205+ 200: t.string,
206+ },
207+ })
208+ }
209+ });` ,
210+ } ;
211+
212+ testCase ( 'computed property api spec' , COMPUTED_PROPERTY , '/index.ts' , {
129213 test : [
130214 {
131215 path : '/test' ,
0 commit comments