From 30590e1bf81a2c231fceeabd9ee1c45df5a27ec3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn?= Date: Thu, 22 Dec 2016 22:16:59 +0100 Subject: [PATCH] Add optics-agent typings (#13457) * Add optics-agent * Remove namespace and use default tslint tsconfig * Set no implicit this to true --- optics-agent/index.d.ts | 85 ++++++++++++++++++++++++++++++ optics-agent/optics-agent-tests.ts | 37 +++++++++++++ optics-agent/tsconfig.json | 20 +++++++ optics-agent/tslint.json | 1 + 4 files changed, 143 insertions(+) create mode 100644 optics-agent/index.d.ts create mode 100644 optics-agent/optics-agent-tests.ts create mode 100644 optics-agent/tsconfig.json create mode 100644 optics-agent/tslint.json diff --git a/optics-agent/index.d.ts b/optics-agent/index.d.ts new file mode 100644 index 0000000000..ba1e930739 --- /dev/null +++ b/optics-agent/index.d.ts @@ -0,0 +1,85 @@ +// Type definitions for optics-agent 1.0 +// Project: https://github.com/apollostack/optics-agent-js#readme +// Definitions by: Crevil +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +import { GraphQLSchema } from "graphql"; +import { Request, Response } from "express"; +import { Server } from "hapi"; + +export interface Options { + /** + * Your API key for the Optics service. + * + * This defaults to the OPTICS_API_KEY environment variable, but can be overridden here. + */ + apiKey?: string; + + /** + * Called to determine the query shape for for a GraphQL query. + * + * You shouldn't need to set this unless you are debugging. + */ + normalizeQuery?: (graphQLResolveInfo: any) => string; + + /** + * Where to send the reports. Defaults to the production Optics endpoint, or the OPTICS_ENDPOINT_URL environment variable if it is set. + * + * You shouldn't need to set this unless you are debugging. + */ + endpointUrl?: string; + + /** + * HTTP proxy to use when sending reports. Default to no proxying, or the HTTPS_PROXY environment variable if it is set. + * + * You should only set this when your servers cannot connect directly to the Optics service. + */ + proxyUrl?: string; + + /** + * How often to send reports in milliseconds. Defaults to 1 minute. Minimum 10 seconds. + * + * You shouldn't need to set this unless you are debugging. + */ + reportIntervalMs?: number; + + /** + * Print a JSON version of reports as they are sent. This may be useful for debugging. Defaults to false. + */ + printReports?: boolean; + + /** + * Send detailed traces along with usage reports. Defaults to true. + */ + reportTraces?: boolean; + + /** + * Send the query variables along with traces. Defaults to true. + */ + reportVariables?: boolean; +} + +export class Agent { + constructor(options: Options); + + configureAgent(options: Options): Agent; + instrumentSchema(schema: GraphQLSchema): void; + middleware(): (req: Request, res: Response, next?: any) => void; + instrumentHapiServer(server: Server): void; + context(req: Request): any; +} + +export function configureAgent(options: Options): Agent; +export function instrumentSchema(schema: GraphQLSchema): void; +export function middleware(): (req: Request, res: Response, next?: any) => void; +export function instrumentHapiServer(server: Server): void; +export function context(req: Request): any; + +export default { + configureAgent, + instrumentSchema, + middleware, + instrumentHapiServer, + context, + Agent, +}; diff --git a/optics-agent/optics-agent-tests.ts b/optics-agent/optics-agent-tests.ts new file mode 100644 index 0000000000..75fafbd871 --- /dev/null +++ b/optics-agent/optics-agent-tests.ts @@ -0,0 +1,37 @@ +import OpticsAgent, { + configureAgent, + instrumentSchema, + middleware, + instrumentHapiServer, + context, +} from 'optics-agent'; +import { GraphQLSchema } from 'graphql'; +import * as express from 'express'; +import * as hapi from 'hapi'; +var OpticsAgentRequired = require('optics-agent'); + +const configOptions = { + apiKey: "", + reportTraces: false, + reportVariables: false, + printReports: false, + normalizeQuery: (info: any) => "", + endpointUrl: "", + proxyUrl: "", + reportIntervalMs: 1, +}; +OpticsAgent.configureAgent(configOptions) + +let expressServer = express(); +expressServer.use(OpticsAgent.middleware()); + +let hapiServer = new hapi.Server(); +OpticsAgent.instrumentHapiServer(hapiServer); + +let req = {} as express.Request; +OpticsAgent.context(req); + +const agent = new OpticsAgent.Agent({ apiKey: '1234' }); + +let schema = {} as GraphQLSchema; +agent.instrumentSchema(schema); diff --git a/optics-agent/tsconfig.json b/optics-agent/tsconfig.json new file mode 100644 index 0000000000..09833df249 --- /dev/null +++ b/optics-agent/tsconfig.json @@ -0,0 +1,20 @@ +{ + "compilerOptions": { + "module": "commonjs", + "target": "es6", + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "optics-agent-tests.ts" + ] +} diff --git a/optics-agent/tslint.json b/optics-agent/tslint.json new file mode 100644 index 0000000000..377cc837d4 --- /dev/null +++ b/optics-agent/tslint.json @@ -0,0 +1 @@ +{ "extends": "../tslint.json" }