Skip to content

Commit

Permalink
Merge pull request #33 from jfrconley/joco/refactor-exports
Browse files Browse the repository at this point in the history
refactor exports
  • Loading branch information
jfrconley authored Jul 29, 2024
2 parents 6e5301d + 26fdadf commit 3b704cd
Show file tree
Hide file tree
Showing 30 changed files with 2,440 additions and 2,887 deletions.
5 changes: 5 additions & 0 deletions .changeset/stupid-tomatoes-join.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nornir/rest": minor
---

Refactor exports
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"syncpack": "^9.8.4",
"ts-patch": "^3.1.1",
"turbo": "^1.9.2",
"typescript": "^5.4.2"
"typescript": "5.4.2"
},
"engines": {
"node": ">=18.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"eslint": "^8.45.0",
"jest": "^29.5.0",
"ts-patch": "^3.1.1",
"typescript": "^5.4.2"
"typescript": "5.4.2"
},
"engines": {
"node": ">=18.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/rest/__tests__/src/openapi-router.spec.mts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {OpenAPIV3_1} from "../../dist/runtime/index.mjs";
import {describe, expect} from "@jest/globals";
import {OpenAPIRouter} from "../../dist/runtime/openapi/openapi-router.mjs"
import {OpenAPIRouter} from "../../dist/runtime/openapi-router/openapi-router.mjs"
import {AttachmentRegistry} from "@nornir/core";

const TestSpec = {
Expand Down
2 changes: 1 addition & 1 deletion packages/rest/__tests__/src/routing.spec.mts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
} from "../../dist/runtime/index.mjs";
import {nornir, Nornir} from "@nornir/core";
import {describe} from "@jest/globals";
import {NornirRouteNotFoundError} from "../../dist/runtime/error.mjs";
import {NornirRouteNotFoundError} from "../../dist/runtime/shared/index.mjs";

interface RouteGetInput extends HttpRequestEmpty {
}
Expand Down
2 changes: 1 addition & 1 deletion packages/rest/__tests__/src/utils.spec.mts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {describe, expect} from "@jest/globals";
import {simpleSpecResolve} from "../../dist/runtime/utils.mjs";
import {simpleSpecResolve} from "../../dist/runtime/shared/utils.mjs";

describe("utils", () => {
describe('simpleSpecResolve function', () => {
Expand Down
6 changes: 1 addition & 5 deletions packages/rest/__tests__/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@
"jest",
"node"
],
"paths": {
"@nrfcloud/utils-public.aws/*": [
"../dist"
]
}
"paths": {}
},
"include": [
"src/**/*"
Expand Down
15 changes: 13 additions & 2 deletions packages/rest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
"@nrfcloud/ts-json-schema-transformer": "^1.4.2",
"@types/aws-lambda": "^8.10.115",
"ajv": "^8.16.0",
"ajv-formats": "^3.0.1",
"atlassian-openapi": "^1.0.18",
"debug": "^4.3.4",
"glob": "^10.3.10",
Expand All @@ -38,7 +37,7 @@
"eslint": "^8.45.0",
"jest": "^29.5.0",
"ts-patch": "^3.1.1",
"typescript": "^5.4.2"
"typescript": "5.4.2"
},
"engines": {
"node": ">=18.0.0",
Expand All @@ -50,6 +49,18 @@
"types": "./dist/runtime/index.d.mts",
"import": "./dist/runtime/index.mjs"
},
"./decorator-router": {
"types": "./dist/runtime/decorator-router/index.d.mts",
"import": "./dist/runtime/decorator-router/index.mjs"
},
"./lib": {
"types": "./dist/runtime/shared/index.d.mts",
"import": "./dist/runtime/shared/index.mjs"
},
"./openapi-router": {
"types": "./dist/runtime/openapi-router/index.d.mts",
"import": "./dist/runtime/openapi-router/index.mjs"
},
"./transform": {
"require": "./dist/transform/transform.js"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Nornir} from "@nornir/core";
import {HttpRequest, HttpResponse, HttpStatusCode, MimeType} from "./http-event.mjs";
import {HttpRequest, HttpResponse, HttpStatusCode, MimeType} from "../shared/http-event.mjs";
import {InstanceOf} from "ts-morph";

const UNTRANSFORMED_ERROR = new Error("nornir/rest decorators have not been transformed. Have you setup ts-patch/ttypescript and added the originator to your tsconfig.json?");
Expand Down
6 changes: 6 additions & 0 deletions packages/rest/src/runtime/decorator-router/index.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export {Router} from "./router.mjs"
export {
GetChain, Controller, PostChain, DeleteChain, HeadChain, OptionsChain, PatchChain, PutChain,
Provider, ValidateRequestType, ValidateResponseType
} from './decorators.mjs';
export {RouteHolder} from "./route-holder.mjs"
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import {HttpMethod, HttpRequest, HttpResponse, HttpStatusCode, MimeType} from './http-event.mjs';
import {HttpMethod, HttpRequest, HttpResponse} from '../shared/http-event.mjs';
import {Nornir} from '@nornir/core';
import {NornirRestRequestError} from './error.mjs';
import {type ErrorObject, type ValidateFunction} from 'ajv'
import {debugLog} from "./utils.mjs";
import {NornirRestRequestValidationError} from '../shared/error.mjs';
import {type ValidateFunction} from 'ajv'

export type RouteBuilder<Input extends HttpRequest = HttpRequest, Output extends HttpResponse = HttpResponse> = (chain: Nornir<Input>) => Nornir<Input, Output>

Expand Down Expand Up @@ -35,27 +34,3 @@ export class RouteHolder {
})
}
}

/**
* Error thrown when the request does not pass validation checks.
* Includes information about the validation errors.
*/
export class NornirRestRequestValidationError<Request extends HttpRequest> extends NornirRestRequestError<Request> {
constructor(
request: Request,
public readonly errors: ErrorObject[]
) {
super(request, `Request validation failed`)
debugLog(`Request validation failed`, {request, errors})
}

toHttpResponse(): HttpResponse {
return {
statusCode: HttpStatusCode.UnprocessableEntity,
body: {errors: this.errors},
headers: {
'content-type': MimeType.ApplicationJson
},
}
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import Trouter from 'trouter';
import {RouteBuilder, RouteHolder} from './route-holder.mjs';
import {HttpEvent, HttpHeadersWithContentType, HttpMethod, HttpRequest, HttpResponse} from './http-event.mjs';
import {HttpEvent, HttpHeadersWithContentType, HttpMethod, HttpRequest, HttpResponse} from '../shared/http-event.mjs';
import {AttachmentRegistry, Nornir, Result} from '@nornir/core';
import {NornirRouteNotFoundError} from "./error.mjs";
import {NornirRouteNotFoundError} from "../shared/error.mjs";

type RouteHandler = (request: Result<HttpRequest>, registry: AttachmentRegistry) => Promise<Result<HttpResponse>>;

const instanceMap = new Map<string, Router>();

export class Router {
private static DEFAULT_INSTANCE_ID = 'default';
private static readonly instanceMap = new Map<string, Router>();
private static readonly instanceMap = instanceMap;

/** @internal */
public static get(apiId?: string): Router {
Expand Down
38 changes: 12 additions & 26 deletions packages/rest/src/runtime/index.mts
Original file line number Diff line number Diff line change
@@ -1,32 +1,18 @@
import {Router} from "./decorator-router/index.mjs";
import {nornir} from "@nornir/core";
import {UnparsedHttpEvent} from './http-event.mjs';
import {normalizeEventHeaders} from "./utils.mjs"
import {httpEventParser} from './parse.mjs'
import {httpResponseSerializer} from './serialize.mjs'
import {
httpErrorHandler,
httpEventParser,
httpResponseSerializer,
normalizeEventHeaders,
UnparsedHttpEvent
} from "./shared/index.mjs";
import {OpenAPIRouter} from "./openapi-router/index.mjs";

import {Router} from './router.mjs';
import {httpErrorHandler} from "./error.mjs";
export * from "./shared/index.mjs";
export * from "./openapi-router/index.mjs";
export * from "./decorator-router/index.mjs";

export {
GetChain, Controller, PostChain, DeleteChain, HeadChain, OptionsChain, PatchChain, PutChain,
Provider, ValidateRequestType, ValidateResponseType
} from './decorators.mjs';
export {
HttpResponse, HttpRequest, HttpEvent, HttpMethod, HttpRequestEmpty, HttpResponseEmpty,
HttpStatusCode, HttpRequestJSON, HttpHeaders, MimeType,
UnparsedHttpEvent, SerializedHttpResponse
} from './http-event.mjs';
export {RouteHolder, NornirRestRequestValidationError} from './route-holder.mjs'
export {NornirRestRequestError, NornirRestError, httpErrorHandler, mapError, mapErrorClass,NornirRouteNotFoundError} from './error.mjs'
export {ApiGatewayProxyV2, startLocalServer} from "./converters.mjs"
export {httpEventParser, HttpBodyParser, HttpBodyParserMap, HttpQueryStringParser, NornirRestParseError} from "./parse.mjs"
export {httpResponseSerializer, HttpBodySerializer, HttpBodySerializerMap} from "./serialize.mjs"
export {normalizeEventHeaders, normalizeHeaders, getContentType} from "./utils.mjs"
export {Router} from "./router.mjs"

import {OpenAPIRouter} from "./openapi/openapi-router.mjs"
export {OpenAPIRouter} from "./openapi/openapi-router.mjs"
export {OpenAPIV3_1} from "./openapi/spec.mjs"

export const router = Router.build

Expand Down
Loading

0 comments on commit 3b704cd

Please sign in to comment.