Skip to content

Commit

Permalink
feat: quick rest api with moquerie.rest.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
Akryum committed Sep 18, 2024
1 parent 71e159b commit 42ffe18
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 2 deletions.
26 changes: 24 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Install the `moquerie` package:
pnpm install moquerie
```

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

```ts
import { defineConfig } from 'moquerie/config'
Expand All @@ -57,9 +57,31 @@ export default defineConfig({
})
```

## Getting started

### REST Quickstart

*(Optional)* To quickly create a fake REST server, create a `moquerie.rest.ts` file in your project that export resource types:

```ts
// moquerie.rest.ts

export interface MyObject {
id: string
title: string
count: number
}
```

Moquerie will detect this file and automatically create RESTful endpoints for each resource type found within (without any additional configuration).

### GraphQL Quickstart

If you have a GraphQL schema, you can let moquerie scan your code files for graphql schema definitions that uses the `gql` tag.

```ts
// moquerie.config.ts

import { defineConfig } from 'moquerie/config'

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

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

## Getting started
### Run the server

Run the `moquerie` command to start the server:

Expand Down
13 changes: 13 additions & 0 deletions packages/core/src/config/resolve.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import fs from 'node:fs'
import path from 'pathe'
import { loadConfig } from 'c12'
import type { Config } from '../types/config.js'

export async function resolveConfig(cwd: string) {
// Auto Rest API Types
const autoRestTypesFile = path.resolve(cwd, 'moquerie.rest.ts')
const autoRestTypesEnabled = fs.existsSync(autoRestTypesFile)

return loadConfig<Config>({
name: 'moquerie',
cwd,
Expand All @@ -16,6 +22,13 @@ export async function resolveConfig(cwd: string) {
'**/*.mock.js',
'**/*.mock.ts',
],
...autoRestTypesEnabled
? {
rest: {
typeFiles: [autoRestTypesFile],
},
}
: {},
},
})
}
10 changes: 10 additions & 0 deletions playgrounds/quick-rest/.moquerie/types/queryManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { QueryManager } from "moquerie";

Check failure on line 1 in playgrounds/quick-rest/.moquerie/types/queryManager.ts

View workflow job for this annotation

GitHub Actions / Build and test

All imports in the declaration are only used as types. Use `import type`

Check failure on line 1 in playgrounds/quick-rest/.moquerie/types/queryManager.ts

View workflow job for this annotation

GitHub Actions / Build and test

Strings must use singlequote

Check failure on line 1 in playgrounds/quick-rest/.moquerie/types/queryManager.ts

View workflow job for this annotation

GitHub Actions / Build and test

Extra semicolon
import { Message, MyObject, User } from "./resources.js";

Check failure on line 2 in playgrounds/quick-rest/.moquerie/types/queryManager.ts

View workflow job for this annotation

GitHub Actions / Build and test

All imports in the declaration are only used as types. Use `import type`

Check failure on line 2 in playgrounds/quick-rest/.moquerie/types/queryManager.ts

View workflow job for this annotation

GitHub Actions / Build and test

Strings must use singlequote

Check failure on line 2 in playgrounds/quick-rest/.moquerie/types/queryManager.ts

View workflow job for this annotation

GitHub Actions / Build and test

Extra semicolon

declare module "moquerie" {

Check failure on line 4 in playgrounds/quick-rest/.moquerie/types/queryManager.ts

View workflow job for this annotation

GitHub Actions / Build and test

Strings must use singlequote
export interface QueryManagerProxy {

Check failure on line 5 in playgrounds/quick-rest/.moquerie/types/queryManager.ts

View workflow job for this annotation

GitHub Actions / Build and test

Expected indentation of 2 spaces but found 4
Message: QueryManager<Message>;

Check failure on line 6 in playgrounds/quick-rest/.moquerie/types/queryManager.ts

View workflow job for this annotation

GitHub Actions / Build and test

Expected indentation of 4 spaces but found 8

Check failure on line 6 in playgrounds/quick-rest/.moquerie/types/queryManager.ts

View workflow job for this annotation

GitHub Actions / Build and test

Unexpected separator (;)
MyObject: QueryManager<MyObject>;
User: QueryManager<User>;
}
}
18 changes: 18 additions & 0 deletions playgrounds/quick-rest/.moquerie/types/resources.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export interface Message {
id: string;
text: string;
user: User;
}

export interface MyObject {
id: string;
count: number;
title: string;
}

export interface User {
id: string;
email: string;
messages: Message;
name: string;
}
18 changes: 18 additions & 0 deletions playgrounds/quick-rest/moquerie.rest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export interface MyObject {
id: string
title: string
count: number
}

export interface User {
id: string
name: string
email: string
messages: Message[]
}

export interface Message {
id: string
text: string
user: User
}
13 changes: 13 additions & 0 deletions playgrounds/quick-rest/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "moquerie-playground-quick-rest",
"type": "module",
"version": "0.0.0",
"private": true,
"scripts": {
"moquerie": "moquerie"
},
"devDependencies": {
"moquerie": "workspace:*",
"typescript": "^5.2.2"
}
}
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 42ffe18

Please sign in to comment.