Skip to content

Commit 42ffe18

Browse files
committed
feat: quick rest api with moquerie.rest.ts
1 parent 71e159b commit 42ffe18

File tree

7 files changed

+105
-2
lines changed

7 files changed

+105
-2
lines changed

README.md

+24-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ Install the `moquerie` package:
4444
pnpm install moquerie
4545
```
4646

47-
Create a `moquerie.config.ts` (or `moquerie.config.js`) file in the root of your project:
47+
*(Optional)* Create a `moquerie.config.ts` (or `moquerie.config.js`) file in the root of your project:
4848

4949
```ts
5050
import { defineConfig } from 'moquerie/config'
@@ -57,9 +57,31 @@ export default defineConfig({
5757
})
5858
```
5959

60+
## Getting started
61+
62+
### REST Quickstart
63+
64+
*(Optional)* To quickly create a fake REST server, create a `moquerie.rest.ts` file in your project that export resource types:
65+
66+
```ts
67+
// moquerie.rest.ts
68+
69+
export interface MyObject {
70+
id: string
71+
title: string
72+
count: number
73+
}
74+
```
75+
76+
Moquerie will detect this file and automatically create RESTful endpoints for each resource type found within (without any additional configuration).
77+
78+
### GraphQL Quickstart
79+
6080
If you have a GraphQL schema, you can let moquerie scan your code files for graphql schema definitions that uses the `gql` tag.
6181

6282
```ts
83+
// moquerie.config.ts
84+
6385
import { defineConfig } from 'moquerie/config'
6486

6587
export default defineConfig({
@@ -85,7 +107,7 @@ You also have several options to configure your GraphQL schema:
85107

86108
For REST you don't need additional configuration, but your need to register API Routes.
87109

88-
## Getting started
110+
### Run the server
89111

90112
Run the `moquerie` command to start the server:
91113

packages/core/src/config/resolve.ts

+13
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1+
import fs from 'node:fs'
2+
import path from 'pathe'
13
import { loadConfig } from 'c12'
24
import type { Config } from '../types/config.js'
35

46
export async function resolveConfig(cwd: string) {
7+
// Auto Rest API Types
8+
const autoRestTypesFile = path.resolve(cwd, 'moquerie.rest.ts')
9+
const autoRestTypesEnabled = fs.existsSync(autoRestTypesFile)
10+
511
return loadConfig<Config>({
612
name: 'moquerie',
713
cwd,
@@ -16,6 +22,13 @@ export async function resolveConfig(cwd: string) {
1622
'**/*.mock.js',
1723
'**/*.mock.ts',
1824
],
25+
...autoRestTypesEnabled
26+
? {
27+
rest: {
28+
typeFiles: [autoRestTypesFile],
29+
},
30+
}
31+
: {},
1932
},
2033
})
2134
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { QueryManager } from "moquerie";
2+
import { Message, MyObject, User } from "./resources.js";
3+
4+
declare module "moquerie" {
5+
export interface QueryManagerProxy {
6+
Message: QueryManager<Message>;
7+
MyObject: QueryManager<MyObject>;
8+
User: QueryManager<User>;
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
export interface Message {
2+
id: string;
3+
text: string;
4+
user: User;
5+
}
6+
7+
export interface MyObject {
8+
id: string;
9+
count: number;
10+
title: string;
11+
}
12+
13+
export interface User {
14+
id: string;
15+
email: string;
16+
messages: Message;
17+
name: string;
18+
}
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
export interface MyObject {
2+
id: string
3+
title: string
4+
count: number
5+
}
6+
7+
export interface User {
8+
id: string
9+
name: string
10+
email: string
11+
messages: Message[]
12+
}
13+
14+
export interface Message {
15+
id: string
16+
text: string
17+
user: User
18+
}

playgrounds/quick-rest/package.json

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "moquerie-playground-quick-rest",
3+
"type": "module",
4+
"version": "0.0.0",
5+
"private": true,
6+
"scripts": {
7+
"moquerie": "moquerie"
8+
},
9+
"devDependencies": {
10+
"moquerie": "workspace:*",
11+
"typescript": "^5.2.2"
12+
}
13+
}

pnpm-lock.yaml

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)