From 42ffe1857d966a8c151f2453446078f5e91a1085 Mon Sep 17 00:00:00 2001 From: Guillaume Chau Date: Wed, 18 Sep 2024 17:30:42 +0200 Subject: [PATCH] feat: quick rest api with moquerie.rest.ts --- README.md | 26 +++++++++++++++++-- packages/core/src/config/resolve.ts | 13 ++++++++++ .../.moquerie/types/queryManager.ts | 10 +++++++ .../quick-rest/.moquerie/types/resources.ts | 18 +++++++++++++ playgrounds/quick-rest/moquerie.rest.ts | 18 +++++++++++++ playgrounds/quick-rest/package.json | 13 ++++++++++ pnpm-lock.yaml | 9 +++++++ 7 files changed, 105 insertions(+), 2 deletions(-) create mode 100644 playgrounds/quick-rest/.moquerie/types/queryManager.ts create mode 100644 playgrounds/quick-rest/.moquerie/types/resources.ts create mode 100644 playgrounds/quick-rest/moquerie.rest.ts create mode 100644 playgrounds/quick-rest/package.json diff --git a/README.md b/README.md index 0a69baa..6398c1a 100644 --- a/README.md +++ b/README.md @@ -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' @@ -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({ @@ -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: diff --git a/packages/core/src/config/resolve.ts b/packages/core/src/config/resolve.ts index 1c21936..f9e8fd0 100644 --- a/packages/core/src/config/resolve.ts +++ b/packages/core/src/config/resolve.ts @@ -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({ name: 'moquerie', cwd, @@ -16,6 +22,13 @@ export async function resolveConfig(cwd: string) { '**/*.mock.js', '**/*.mock.ts', ], + ...autoRestTypesEnabled + ? { + rest: { + typeFiles: [autoRestTypesFile], + }, + } + : {}, }, }) } diff --git a/playgrounds/quick-rest/.moquerie/types/queryManager.ts b/playgrounds/quick-rest/.moquerie/types/queryManager.ts new file mode 100644 index 0000000..394e439 --- /dev/null +++ b/playgrounds/quick-rest/.moquerie/types/queryManager.ts @@ -0,0 +1,10 @@ +import { QueryManager } from "moquerie"; +import { Message, MyObject, User } from "./resources.js"; + +declare module "moquerie" { + export interface QueryManagerProxy { + Message: QueryManager; + MyObject: QueryManager; + User: QueryManager; + } +} \ No newline at end of file diff --git a/playgrounds/quick-rest/.moquerie/types/resources.ts b/playgrounds/quick-rest/.moquerie/types/resources.ts new file mode 100644 index 0000000..035bad6 --- /dev/null +++ b/playgrounds/quick-rest/.moquerie/types/resources.ts @@ -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; +} \ No newline at end of file diff --git a/playgrounds/quick-rest/moquerie.rest.ts b/playgrounds/quick-rest/moquerie.rest.ts new file mode 100644 index 0000000..12e4191 --- /dev/null +++ b/playgrounds/quick-rest/moquerie.rest.ts @@ -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 +} diff --git a/playgrounds/quick-rest/package.json b/playgrounds/quick-rest/package.json new file mode 100644 index 0000000..b4f9f24 --- /dev/null +++ b/playgrounds/quick-rest/package.json @@ -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" + } +} diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index f752622..568a863 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -272,6 +272,15 @@ importers: specifier: ^2.1.1 version: 2.1.1(@types/node@20.16.5)(terser@5.33.0) + playgrounds/quick-rest: + devDependencies: + moquerie: + specifier: workspace:* + version: link:../../packages/moquerie + typescript: + specifier: ^5.2.2 + version: 5.6.2 + packages: '@akryum/sheep@0.5.2':