-
-
Notifications
You must be signed in to change notification settings - Fork 111
merge dev to main (v2.14.2) #2113
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { DbClientContract } from '@zenstackhq/runtime'; | ||
import { HttpException, Inject, Injectable, Scope } from "@nestjs/common"; | ||
import { HttpAdapterHost, REQUEST } from "@nestjs/core"; | ||
import { loadAssets } from "../shared"; | ||
import { RPCApiHandler } from '../api/rpc'; | ||
import { ENHANCED_PRISMA } from "./zenstack.constants"; | ||
import { ApiHandlerOptions } from './interfaces'; | ||
|
||
/** | ||
* The ZenStack API handler service for NestJS. The service is used to handle API requests | ||
* and forward them to the ZenStack API handler. It is platform agnostic and can be used | ||
* with any HTTP adapter. | ||
*/ | ||
@Injectable({ scope: Scope.REQUEST }) | ||
export class ApiHandlerService { | ||
constructor( | ||
private readonly httpAdapterHost: HttpAdapterHost, | ||
@Inject(ENHANCED_PRISMA) private readonly prisma: DbClientContract, | ||
@Inject(REQUEST) private readonly request: unknown | ||
) { } | ||
|
||
async handleRequest(options?: ApiHandlerOptions): Promise<unknown> { | ||
const { modelMeta, zodSchemas } = loadAssets(options || {}); | ||
const requestHandler = options?.handler || RPCApiHandler(); | ||
const hostname = this.httpAdapterHost.httpAdapter.getRequestHostname(this.request); | ||
const requestUrl = this.httpAdapterHost.httpAdapter.getRequestUrl(this.request); | ||
// prefix with http:// to make a valid url accepted by URL constructor | ||
const url = new URL(`http://${hostname}${requestUrl}`); | ||
const method = this.httpAdapterHost.httpAdapter.getRequestMethod(this.request); | ||
const path = options?.baseUrl && url.pathname.startsWith(options.baseUrl) ? url.pathname.slice(options.baseUrl.length) : url.pathname; | ||
const searchParams = url.searchParams; | ||
const query = Object.fromEntries(searchParams); | ||
const requestBody = (this.request as { body: unknown }).body; | ||
|
||
const response = await requestHandler({ | ||
method, | ||
path, | ||
query, | ||
requestBody, | ||
prisma: this.prisma, | ||
modelMeta, | ||
zodSchemas, | ||
logger: options?.logger, | ||
}); | ||
|
||
// handle handler error | ||
// if response code >= 400 throw nestjs HttpException | ||
// the error response will be generated by nestjs | ||
// caller can use try/catch to deal with this manually also | ||
if (response.status >= 400) { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
throw new HttpException(response.body as Record<string, any>, response.status) | ||
} | ||
return response.body | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
export * from './zenstack.module'; | ||
export * from './api-handler.service'; | ||
export * from './zenstack.constants'; |
18 changes: 18 additions & 0 deletions
18
packages/server/src/nestjs/interfaces/api-handler-options.interface.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { AdapterBaseOptions } from "../../types"; | ||
|
||
export interface ApiHandlerOptions extends AdapterBaseOptions { | ||
/** | ||
* The base URL for the API handler. This is used to determine the base path for the API requests. | ||
* If you are using the ApiHandlerService in a route with a prefix, you should set this to the prefix. | ||
* | ||
* e.g. | ||
* without baseUrl(API handler default route): | ||
* - RPC API handler: [model]/findMany | ||
* - RESTful API handler: /:type | ||
* | ||
* with baseUrl(/api/crud): | ||
* - RPC API handler: /api/crud/[model]/findMany | ||
* - RESTful API handler: /api/crud/:type | ||
*/ | ||
baseUrl?: string; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './zenstack-module-options.interface' | ||
export * from './api-handler-options.interface' |
41 changes: 41 additions & 0 deletions
41
packages/server/src/nestjs/interfaces/zenstack-module-options.interface.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { FactoryProvider, ModuleMetadata, Provider } from "@nestjs/common"; | ||
|
||
/** | ||
* ZenStack module options. | ||
*/ | ||
export interface ZenStackModuleOptions { | ||
/** | ||
* A callback for getting an enhanced `PrismaClient`. | ||
*/ | ||
getEnhancedPrisma: (model?: string | symbol) => unknown; | ||
} | ||
|
||
/** | ||
* ZenStack module async registration options. | ||
*/ | ||
export interface ZenStackModuleAsyncOptions extends Pick<ModuleMetadata, 'imports'> { | ||
/** | ||
* Whether the module is global-scoped. | ||
*/ | ||
global?: boolean; | ||
|
||
/** | ||
* The token to export the enhanced Prisma service. Default is {@link ENHANCED_PRISMA}. | ||
*/ | ||
exportToken?: string; | ||
|
||
/** | ||
* The factory function to create the enhancement options. | ||
*/ | ||
useFactory: (...args: unknown[]) => Promise<ZenStackModuleOptions> | ZenStackModuleOptions; | ||
|
||
/** | ||
* The dependencies to inject into the factory function. | ||
*/ | ||
inject?: FactoryProvider['inject']; | ||
|
||
/** | ||
* Extra providers to facilitate dependency injection. | ||
*/ | ||
extraProviders?: Provider[]; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/** | ||
* The default token used to export the enhanced Prisma service. | ||
*/ | ||
export const ENHANCED_PRISMA = 'ENHANCED_PRISMA'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.