Skip to content

Commit

Permalink
Remove interface usage from types package for Node
Browse files Browse the repository at this point in the history
All Node.js specific interfaces from the types package are now defined
inside nodejs core package. There's still a dependency from types
package for common types as `Func`, `HashMap`, and `HasMapValue`.

With this change we keep taking advantage of interfaces but now they're
defined in a place where they're used.
  • Loading branch information
luismiramirez committed Oct 6, 2021
1 parent d24ccf4 commit 9c8319e
Show file tree
Hide file tree
Showing 41 changed files with 480 additions and 118 deletions.
22 changes: 12 additions & 10 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion packages/apollo-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"types": "dist/index",
"license": "MIT",
"dependencies": {
"@appsignal/types": "^2.1.5",
"@appsignal/nodejs": "^2.1.0",
"apollo-server-plugin-base": "^0.10.3",
"tslib": "^2.0.3"
},
Expand Down
4 changes: 2 additions & 2 deletions packages/apollo-server/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { NodeClient } from "@appsignal/types"
import { Client } from "@appsignal/nodejs"
import { ApolloServerPlugin } from "apollo-server-plugin-base"

export const createApolloPlugin = (appsignal: NodeClient, _options = {}) => {
export const createApolloPlugin = (appsignal: Client, _options = {}) => {
const tracer = appsignal.tracer()

return (): ApolloServerPlugin => ({
Expand Down
2 changes: 1 addition & 1 deletion packages/express/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"types": "dist/index",
"license": "MIT",
"dependencies": {
"@appsignal/types": "^2.1.5",
"@appsignal/nodejs": "^2.1.0",
"tslib": "^2.0.3"
},
"peerDependencies": {
Expand Down
8 changes: 3 additions & 5 deletions packages/express/src/middleware/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { NodeClient } from "@appsignal/types"
import { Client } from "@appsignal/nodejs"

import {
Request,
Expand All @@ -12,7 +12,7 @@ import {
* Returns an Express middleware that can augment the current span
* with data.
*/
export function expressMiddleware(appsignal: NodeClient): RequestHandler {
export function expressMiddleware(appsignal: Client): RequestHandler {
return function (req: Request, res: Response, next: NextFunction) {
const tracer = appsignal.tracer()
const rootSpan = tracer.currentSpan()
Expand Down Expand Up @@ -57,9 +57,7 @@ export function expressMiddleware(appsignal: NodeClient): RequestHandler {
}
}

export function expressErrorHandler(
appsignal: NodeClient
): ErrorRequestHandler {
export function expressErrorHandler(appsignal: Client): ErrorRequestHandler {
return function (
err: Error & { status?: number },
req: Request,
Expand Down
1 change: 1 addition & 0 deletions packages/koa/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"license": "MIT",
"dependencies": {
"@appsignal/types": "^2.1.5",
"@appsignal/nodejs": "^2.1.0",
"shimmer": "^1.2.1",
"tslib": "^2.0.3"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/koa/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import shimmer from "shimmer"

import koa from "koa"
import type { Tracer, Plugin } from "@appsignal/types"
import type { Tracer, Plugin } from "@appsignal/nodejs"
import { getKoaUsePatch } from "./patches"

export const PLUGIN_NAME = "koa"
Expand Down
2 changes: 1 addition & 1 deletion packages/koa/src/patches/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Copyright 2019, OpenTelemetry Authors
*/

import type { Tracer } from "@appsignal/types"
import type { Tracer } from "@appsignal/nodejs"
import koa, { Middleware, ParameterizedContext, DefaultState } from "koa"
import type { RouterParamContext } from "@koa/router"
import type * as Router from "@koa/router"
Expand Down
2 changes: 1 addition & 1 deletion packages/nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"types": "dist/index",
"license": "MIT",
"dependencies": {
"@appsignal/types": "^2.1.5",
"@appsignal/nodejs": "^2.1.0",
"tslib": "^2.0.3"
},
"peerDependencies": {
Expand Down
4 changes: 2 additions & 2 deletions packages/nextjs/src/experimental.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import url from "url"
import { IncomingMessage, ServerResponse } from "http"
import { NodeClient, Metrics } from "@appsignal/types"
import { Client, Metrics } from "@appsignal/nodejs"

function handleWebVital(
metric: { name: string; value: number },
Expand Down Expand Up @@ -45,7 +45,7 @@ function handleNextMetric(
* Usage of this function is EXPERIMENTAL and may change or be deprecated
* in future releases.
*/
export function getWebVitalsHandler(appsignal: NodeClient) {
export function getWebVitalsHandler(appsignal: Client) {
return function (
req: IncomingMessage,
res: ServerResponse,
Expand Down
4 changes: 2 additions & 2 deletions packages/nextjs/src/handler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import url from "url"
import { NodeClient } from "@appsignal/types"
import { Client } from "@appsignal/nodejs"
import { ServerResponse, IncomingMessage } from "http"

interface NextServer {
Expand Down Expand Up @@ -33,7 +33,7 @@ interface NextServer {
* span with data.
*/
export function getRequestHandler<T extends NextServer>(
appsignal: NodeClient,
appsignal: Client,
app: T
) {
const handler = app.getRequestHandler()
Expand Down
12 changes: 12 additions & 0 deletions packages/nodejs/.changesets/[email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
bump: "minor"
---

Remove interface usage from @appsignal/types

All Node.js-specific interfaces from the types package are now defined
inside the nodejs core package. There's still a dependency from types
package for common types as Func, HashMap, and HasMapValue.

With this change, we keep taking advantage of interfaces, but now they're
defined in a place where they're used.
2 changes: 1 addition & 1 deletion packages/nodejs/src/__tests__/bootstrap.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Metrics } from "@appsignal/types"
import { Metrics } from "../interfaces"
import { Instrumentation } from "../instrument"
import { initCorePlugins, initCoreProbes } from "../bootstrap"

Expand Down
14 changes: 7 additions & 7 deletions packages/nodejs/src/__tests__/client.test.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { Client } from "../client"
import { BaseClient } from "../client"
import { BaseTracer as Tracer } from "../tracer"
import { BaseMetrics as Metrics } from "../metrics"
import { NoopTracer, NoopMetrics } from "../noops"

jest.mock("../tracer")

describe("Client", () => {
describe("BaseClient", () => {
const name = "TEST APP"
const apiKey = "TEST_API_KEY"

let client: Client
let client: BaseClient

// enableMinutelyProbes is set to false so we don't leak timers
const DEFAULT_OPTS = { name, apiKey, enableMinutelyProbes: false }

beforeEach(() => {
client = new Client({ ...DEFAULT_OPTS })
client = new BaseClient({ ...DEFAULT_OPTS })
})

it("starts the client", () => {
Expand All @@ -29,7 +29,7 @@ describe("Client", () => {
})

it("starts the client when the active option is true", () => {
client = new Client({ ...DEFAULT_OPTS, active: true })
client = new BaseClient({ ...DEFAULT_OPTS, active: true })
expect(client.isActive).toBeTruthy()
})

Expand All @@ -39,7 +39,7 @@ describe("Client", () => {
})

it("returns a `Tracer` object if the agent is started", () => {
client = new Client({ ...DEFAULT_OPTS, active: true })
client = new BaseClient({ ...DEFAULT_OPTS, active: true })
const tracer = client.tracer()
expect(tracer).toBeInstanceOf(Tracer)
})
Expand All @@ -50,7 +50,7 @@ describe("Client", () => {
})

it("returns a `Metrics` object if the agent is started", () => {
client = new Client({ ...DEFAULT_OPTS, active: true })
client = new BaseClient({ ...DEFAULT_OPTS, active: true })
const meter = client.metrics()
expect(meter).toBeInstanceOf(Metrics)
})
Expand Down
3 changes: 1 addition & 2 deletions packages/nodejs/src/bootstrap.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Metrics } from "@appsignal/types"

import { Metrics } from "./interfaces"
import { Instrumentation } from "./instrument"
import { httpPlugin, httpsPlugin } from "./instrumentation/http"
import * as pgPlugin from "./instrumentation/pg"
Expand Down
6 changes: 2 additions & 4 deletions packages/nodejs/src/client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { NodeClient, Metrics, Plugin, Tracer } from "@appsignal/types"
import { Client, Metrics, Plugin, Tracer, AppsignalOptions } from "./interfaces"

import { Agent } from "./agent"
import { Configuration } from "./config"
Expand All @@ -8,9 +8,7 @@ import { NoopTracer, NoopMetrics } from "./noops"
import { Instrumentation } from "./instrument"
import { initCorePlugins, initCoreProbes } from "./bootstrap"
import { demo } from "./demo"

import { VERSION } from "./version"
import { AppsignalOptions } from "./interfaces/options"

/**
* AppSignal for Node.js's main class.
Expand All @@ -20,7 +18,7 @@ import { AppsignalOptions } from "./interfaces/options"
*
* @class
*/
export class Client implements NodeClient {
export class BaseClient implements Client {
readonly VERSION = VERSION

config: Configuration
Expand Down
2 changes: 1 addition & 1 deletion packages/nodejs/src/demo.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Tracer } from "@appsignal/types"
import { Tracer } from "./interfaces"

/**
* Sends a demonstration/test sample for a exception and a performance issue.
Expand Down
3 changes: 2 additions & 1 deletion packages/nodejs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
* @module Appsignal
*/

export { Client as Appsignal } from "./client"
export { BaseClient as Appsignal } from "./client"
export * from "./interfaces"
export { EXPERIMENTAL } from "./experimental"
3 changes: 2 additions & 1 deletion packages/nodejs/src/instrument.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Plugin, Tracer, Metrics } from "@appsignal/types"
import { Plugin, Tracer, Metrics } from "./interfaces"

import Hook from "require-in-the-middle"
import semver from "semver"

Expand Down
2 changes: 1 addition & 1 deletion packages/nodejs/src/instrumentation/http/http.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Tracer, Plugin } from "@appsignal/types"
import { Tracer, Plugin } from "../../interfaces"
import shimmer from "shimmer"
import http from "http"

Expand Down
2 changes: 1 addition & 1 deletion packages/nodejs/src/instrumentation/http/https.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Tracer, Plugin } from "@appsignal/types"
import { Tracer, Plugin } from "../../interfaces"
import shimmer from "shimmer"
import https from "https"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Copyright 2019, OpenTelemetry Authors
*/

import { Tracer } from "@appsignal/types"
import { Tracer } from "../../../interfaces"
import { parse } from "url"
import { IncomingMessage, ServerResponse } from "http"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Copyright 2019, OpenTelemetry Authors
*/

import { Tracer, NodeSpan } from "@appsignal/types"
import { Tracer, Span } from "../../../interfaces"
import url from "url"
import { IncomingMessage, ClientRequest, RequestOptions } from "http"

Expand Down Expand Up @@ -72,7 +72,7 @@ function outgoingRequestFunction(
urlOrOptions: string | url.URL | RequestOptions,
...args: unknown[]
): ClientRequest {
let span: NodeSpan
let span: Span
let origin: string
let method: string

Expand Down
2 changes: 1 addition & 1 deletion packages/nodejs/src/instrumentation/pg/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Tracer, Plugin } from "@appsignal/types"
import { Tracer, Plugin } from "../../interfaces"
import shimmer from "shimmer"
import pg from "pg"
import { EventEmitter } from "events"
Expand Down
Loading

0 comments on commit 9c8319e

Please sign in to comment.