Skip to content

Commit

Permalink
Add TypeScript definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
esilkensen committed Oct 30, 2019
1 parent 007acb0 commit 1d94af3
Show file tree
Hide file tree
Showing 42 changed files with 1,085 additions and 11 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"mocha": "^6.2.0",
"nock": "^10.0.6",
"sinon": "^4.5.0",
"sinon-chai": "^2.8.0"
"sinon-chai": "^2.8.0",
"tsd": "^0.10.0"
},
"engines": {
"node": ">= 4.x",
Expand Down
71 changes: 71 additions & 0 deletions packages/core/lib/aws-xray.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import * as ec2Plugin from './segments/plugins/ec2_plugin';
import * as ecsPlugin from './segments/plugins/ecs_plugin';
import * as elasticBeanstalkPlugin from './segments/plugins/elastic_beanstalk_plugin';
import * as segmentUtils from './segments/segment_utils';
import * as utils from './utils';
import * as middleware from './middleware/mw_utils';
import Segment = require('./segments/segment');
import Subsegment = require('./segments/attributes/subsegment');
import sqlData = require('./database/sql_data');

export namespace plugins {
const EC2Plugin: typeof ec2Plugin;
const ECSPlugin: typeof ecsPlugin;
const ElasticBeanstalkPlugin: typeof elasticBeanstalkPlugin;

type EC2Plugin = typeof ec2Plugin;
type ECSPlugin = typeof ecsPlugin;
type ElasticBeanstalkPlugin = typeof elasticBeanstalkPlugin;

type EC2Metadata = ec2Plugin.EC2Metadata;
type ECSMetadata = ecsPlugin.ECSMetadata;
type ElasticBeanstalkMetadata = elasticBeanstalkPlugin.ElasticBeanstalkMetadata;

type Plugin = EC2Plugin | ECSPlugin | ElasticBeanstalkPlugin;
}

export function config(plugins: plugins.Plugin[]): void;

export { appendAWSWhitelist, setAWSWhitelist } from './segments/attributes/aws';

export { setStreamingThreshold } from './segments/segment_utils';

export { setLogger, getLogger, Logger } from './logger';

export { setDaemonAddress } from './daemon_config'

export { captureAsyncFunc, captureCallbackFunc, captureFunc } from './capture'

export { captureAWS, captureAWSClient } from './patchers/aws_p';

export { captureHTTPs, captureHTTPsGlobal } from './patchers/http_p';

export { capturePromise } from './patchers/promise_p';

export { utils }

export namespace database {
const SqlData: typeof sqlData;
type SqlData = sqlData;
}

export { middleware }

export {
getNamespace,
resolveSegment,
getSegment,
setSegment,
isAutomaticMode,
enableAutomaticMode,
enableManualMode,
setContextMissingStrategy
} from './context_utils';

export {
Segment,
Subsegment,
segmentUtils as SegmentUtils
}

export type SegmentLike = Segment | Subsegment;
16 changes: 16 additions & 0 deletions packages/core/lib/capture.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import Segment = require('./segments/segment');
import Subsegment = require('./segments/attributes/subsegment');

export function captureFunc<T>(name: string, fcn: (subsegment?: Subsegment) => T, parent?: Segment | Subsegment): T;

export function captureAsyncFunc<T>(
name: string,
fcn: (subsegment?: Subsegment) => T,
parent?: Segment | Subsegment
): T;

export function captureCallbackFunc<S extends any[], T>(
name: string,
fcn: (...args: S) => T,
parent?: Segment | Subsegment
): (...args: S) => T;
21 changes: 21 additions & 0 deletions packages/core/lib/context_utils.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Namespace } from 'continuation-local-storage';
import Segment = require('./segments/segment');
import Subsegment = require('./segments/attributes/subsegment');

export function getNamespace(): Namespace;

export function resolveSegment(segment: Segment | Subsegment): Segment | Subsegment | undefined;

export function getSegment(): Segment | Subsegment | undefined;

export function setSegment(segment: Segment | Subsegment): void;

export function isAutomaticMode(): boolean;

export function enableAutomaticMode(): void;

export function enableManualMode(): void;

export type ContextMissingStrategy = 'LOG_ERROR' | 'RUNTIME_ERROR' | ((msg: string) => void);

export function setContextMissingStrategy(strategy: ContextMissingStrategy): void;
1 change: 1 addition & 0 deletions packages/core/lib/daemon_config.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export function setDaemonAddress(address: string): void;
11 changes: 11 additions & 0 deletions packages/core/lib/database/sql_data.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
declare class SqlData {
database_version?: string;
driver_version?: string;
preparation?: string;
url?: string;
user?: string;

constructor(databaseVer?: string, driverVer?: string, user?: string, url?: string, queryType?: string);
}

export = SqlData;
1 change: 1 addition & 0 deletions packages/core/lib/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './aws-xray';
10 changes: 10 additions & 0 deletions packages/core/lib/logger.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export interface Logger {
debug(...args: any[]): any;
info(...args: any[]): any;
warn(...args: any[]): any;
error(...args: any[]): any;
}

export function setLogger(logObj: Logger): void;

export function getLogger(): Logger;
11 changes: 11 additions & 0 deletions packages/core/lib/middleware/incoming_request_data.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import * as http from 'http';

declare class IncomingRequestData {
request: { [key: string]: any };

constructor(req: http.IncomingMessage);

close(res: http.ServerResponse): void;
}

export = IncomingRequestData;
56 changes: 56 additions & 0 deletions packages/core/lib/middleware/mw_utils.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import * as http from 'http';
import Segment = require('../segments/segment');
import IncomingRequestData = require('./incoming_request_data');

export const defaultName: string | undefined;

export const dynamicNaming: boolean;

export const hostPattern: string | null;

export function enableDynamicNaming(hostPattern?: string): void;

export function processHeaders(req?: Partial<Pick<http.IncomingMessage, 'headers'>>): { [key: string]: string };

export function resolveName(hostHeader?: string): string;

export function resolveSampling(amznTraceHeader: object, segment: Segment, res: http.ServerResponse): void;

export function setDefaultName(name: string): void;

export function disableCentralizedSampling(): void;

export interface BaseRuleConfig {
http_method: string;
url_path: string;
fixed_target: number;
rate: number;
description?: string;
}

export interface RuleConfigV1 extends BaseRuleConfig {
service_name: string;
}

export interface RuleConfigV2 extends BaseRuleConfig {
host: string;
}

export type RuleConfig = RuleConfigV1 | RuleConfigV2;

export interface DefaultRuleConfig {
fixed_target: number;
rate: number;
}

export interface RulesConfig {
version: number;
default: DefaultRuleConfig;
rules?: RuleConfig[];
}

export function setSamplingRules(source: string | RulesConfig): void;

export {
IncomingRequestData
}
38 changes: 38 additions & 0 deletions packages/core/lib/patchers/aws_p.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import * as AWS from 'aws-sdk';
import Segment = require('../segments/segment');
import Subsegment = require('../segments/attributes/subsegment');

export type Callback<D> = (err: AWS.AWSError | undefined, data: D) => void;

export interface AWSRequestMethod<P, D> {
(params: P, callback?: Callback<D>): AWS.Request<D, AWS.AWSError>;
(callback?: Callback<D>): AWS.Request<D, AWS.AWSError>;
}

export type PatchedAWSRequestMethod<P, D> = AWSRequestMethod<P & { XRaySegment?: Segment | Subsegment }, D>;

export type PatchedAWSClient<T extends AWS.Service> = {
[K in keyof T]: T[K] extends AWSRequestMethod<infer P, infer D>
? PatchedAWSRequestMethod<P, D>
: T[K]
};

export interface AWSClientConstructor<P, T extends typeof AWS.Service> {
new(params?: P): InstanceType<T>;
}

export interface PatchedAWSClientConstructor<P, T extends typeof AWS.Service> {
new(params?: P): PatchedAWSClient<InstanceType<T>>;
}

export type PatchedAWS<T = typeof AWS> = {
[K in keyof T]: T[K] extends typeof AWS.Service
? (T[K] extends AWSClientConstructor<infer P, T[K]>
? PatchedAWSClientConstructor<P, T[K]>
: T[K])
: T[K];
};

export function captureAWS(awssdk: typeof AWS): PatchedAWS;

export function captureAWSClient<T extends AWS.Service>(service: T): PatchedAWSClient<T>;
6 changes: 6 additions & 0 deletions packages/core/lib/patchers/http_p.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import * as http from 'http';
import * as https from 'https';

export function captureHTTPs<T extends typeof http | typeof https>(mod: T, downstreamXRayEnabled: boolean): T;

export function captureHTTPsGlobal(mod: typeof https | typeof http, downstreamXRayEnabled: boolean): void;
4 changes: 4 additions & 0 deletions packages/core/lib/patchers/promise_p.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const capturePromise: {
(): void;
patchThirdPartyPromise(Promise: any): void;
};
15 changes: 15 additions & 0 deletions packages/core/lib/segments/attributes/aws.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import * as AWS from "aws-sdk";

declare class Aws {
constructor(res: AWS.Response<any, any>, serviceName: string);

addData(data: any): void;
}

declare namespace Aws {
function setAWSWhitelist(source: string | object): void;

function appendAWSWhitelist(source: string | object): void;
}

export = Aws;
57 changes: 57 additions & 0 deletions packages/core/lib/segments/attributes/subsegment.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import * as http from 'http';

declare class Subsegment {
id: string;
name: string;
start_time: number;
in_progress: boolean;
counter: number;

constructor(name: string);

addNewSubsegment(name: string): Subsegment;

addSubsegment(subsegment: Subsegment): void;

removeSubsegment(subsegment: Subsegment): void;

addAttribute(name: string, data: any): void;

addPrecursorId(id: string): void;

addAnnotation(key: string, value: boolean | string | number): void;

addMetadata(key: string, value: any, namespace?: string): void;

addSqlData(sqlData: any): void;

addError(err: Error | string, remote?: boolean): void;

addRemoteRequestData(req: http.ClientRequest, res: http.IncomingMessage, downstreamXRayEnabled: boolean): void;

addFaultFlag(): void;

addErrorFlag(): void;

addThrottleFlag(): void;

close(err?: Error | string | null, remote?: boolean): void;

incrementCounter(additional?: number): void;

decrementCounter(): void;

isClosed(): boolean;

flush(): void;

streamSubsegments(): true | undefined;

format(): string;

toString(): string;

toJSON(): { [key: string]: any };
}

export = Subsegment;
10 changes: 10 additions & 0 deletions packages/core/lib/segments/plugins/ec2_plugin.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export interface EC2Metadata {
ec2: {
instance_id: string;
availability_zone: string;
};
}

export function getData(callback: (metadata?: EC2Metadata) => void): void;

export const originName: string;
9 changes: 9 additions & 0 deletions packages/core/lib/segments/plugins/ecs_plugin.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export interface ECSMetadata {
ecs: {
container: string;
};
}

export function getData(callback: (metadata?: ECSMetadata) => void): void;

export const originName: string;
11 changes: 11 additions & 0 deletions packages/core/lib/segments/plugins/elastic_beanstalk_plugin.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export interface ElasticBeanstalkMetadata {
elastic_beanstalk: {
environment: string;
version_label: string;
deployment_id: number;
};
}

export function getData(callback: (metadata?: ElasticBeanstalkMetadata) => void): void;

export const originName: string;
Loading

0 comments on commit 1d94af3

Please sign in to comment.