From e613c412a5dd6e2841ad4fcd91a5c60ab2e964df Mon Sep 17 00:00:00 2001 From: Jesse Rosenberger Date: Mon, 13 May 2019 17:31:19 +0300 Subject: [PATCH] chore: Move default client signature generation into its own function. This makes no actual changes to the function, but it moves this rather large block of code out of the `constructor` for `EngineReportingExtension`. --- .../apollo-engine-reporting/src/extension.ts | 53 +++++++++---------- 1 file changed, 26 insertions(+), 27 deletions(-) diff --git a/packages/apollo-engine-reporting/src/extension.ts b/packages/apollo-engine-reporting/src/extension.ts index 4dd207d6a38..7bd2c724c8f 100644 --- a/packages/apollo-engine-reporting/src/extension.ts +++ b/packages/apollo-engine-reporting/src/extension.ts @@ -66,33 +66,7 @@ export class EngineReportingExtension this.trace.root = root; this.nodes.set(responsePathAsString(undefined), root); this.generateClientInfo = - options.generateClientInfo || - // Default to using the `apollo-client-x` header fields if present. - // If none are present, fallback on the `clientInfo` query extension - // for backwards compatibility. - // The default value if neither header values nor query extension is - // set is the empty String for all fields (as per protobuf defaults) - (({ request }) => { - if ( - request.http && - request.http.headers && - (request.http.headers.get(clientNameHeaderKey) || - request.http.headers.get(clientVersionHeaderKey) || - request.http.headers.get(clientReferenceIdHeaderKey)) - ) { - return { - clientName: request.http.headers.get(clientNameHeaderKey), - clientVersion: request.http.headers.get(clientVersionHeaderKey), - clientReferenceId: request.http.headers.get( - clientReferenceIdHeaderKey, - ), - }; - } else if (request.extensions && request.extensions.clientInfo) { - return request.extensions.clientInfo; - } else { - return {}; - } - }); + options.generateClientInfo || defaultGenerateClientInfo; } public requestDidStart(o: { @@ -453,3 +427,28 @@ function dateToTimestamp(date: Date): google.protobuf.Timestamp { function durationHrTimeToNanos(hrtime: [number, number]) { return hrtime[0] * 1e9 + hrtime[1]; } + +function defaultGenerateClientInfo({ request }: GraphQLRequestContext) { + // Default to using the `apollo-client-x` header fields if present. + // If none are present, fallback on the `clientInfo` query extension + // for backwards compatibility. + // The default value if neither header values nor query extension is + // set is the empty String for all fields (as per protobuf defaults) + if ( + request.http && + request.http.headers && + (request.http.headers.get(clientNameHeaderKey) || + request.http.headers.get(clientVersionHeaderKey) || + request.http.headers.get(clientReferenceIdHeaderKey)) + ) { + return { + clientName: request.http.headers.get(clientNameHeaderKey), + clientVersion: request.http.headers.get(clientVersionHeaderKey), + clientReferenceId: request.http.headers.get(clientReferenceIdHeaderKey), + }; + } else if (request.extensions && request.extensions.clientInfo) { + return request.extensions.clientInfo; + } else { + return {}; + } +}