Skip to content
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

refactor exports #33

Merged
merged 3 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 Expand Up @@ -697,7 +697,7 @@
const router2 = OpenAPIRouter.fromSpec(TestSpec);

router2.implementRoute("/root/basepath/route/{reallyCool}", "get", chain =>
chain.use(request => {

Check warning on line 700 in packages/rest/__tests__/src/openapi-router.spec.mts

View workflow job for this annotation

GitHub Actions / Lint Back-end code

'request' is defined but never used. Allowed unused args must match /^_/u
return {
statusCode: "200",
headers: {
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 @@
} 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 Expand Up @@ -74,7 +74,7 @@


@Controller(basePath, "rest")
class TestController {

Check warning on line 77 in packages/rest/__tests__/src/routing.spec.mts

View workflow job for this annotation

GitHub Actions / Lint Back-end code

'TestController' is defined but never used. Allowed unused vars must match /^_/u
/**
* A simple get route
* @summary Cool Route
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 Expand Up @@ -39,7 +39,7 @@
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type HasBody<T extends HttpResponse | HttpRequest> = T extends { body: any } ? true : false

type Test = { statusCode: HttpStatusCode.Ok, headers: NonNullable<unknown>, body: string}

Check warning on line 42 in packages/rest/src/runtime/decorator-router/decorators.mts

View workflow job for this annotation

GitHub Actions / Lint Back-end code

'Test' is defined but never used. Allowed unused vars must match /^_/u

/**
* Use to mark a method as a GET route
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
Loading