diff --git a/packages/core/src/lambda/activation.ts b/packages/core/src/lambda/activation.ts index d799173697e..7c8c02e81e9 100644 --- a/packages/core/src/lambda/activation.ts +++ b/packages/core/src/lambda/activation.ts @@ -24,6 +24,7 @@ import { liveTailRegistry, liveTailCodeLensProvider } from '../awsService/cloudW import { getFunctionLogGroupName } from '../awsService/cloudWatchLogs/activation' import { ToolkitError, isError } from '../shared/errors' import { LogStreamFilterResponse } from '../awsService/cloudWatchLogs/wizard/liveTailLogStreamSubmenu' +import { registerLambdaUriHandler } from './uriHandlers' /** * Activates Lambda components. @@ -116,6 +117,8 @@ export async function activate(context: ExtContext): Promise { throw err } } - }) + }), + + registerLambdaUriHandler() ) } diff --git a/packages/core/src/lambda/uriHandlers.ts b/packages/core/src/lambda/uriHandlers.ts new file mode 100644 index 00000000000..41f56ce1669 --- /dev/null +++ b/packages/core/src/lambda/uriHandlers.ts @@ -0,0 +1,44 @@ +/*! + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ + +import * as vscode from 'vscode' +import * as nls from 'vscode-nls' + +import { SearchParams } from '../shared/vscode/uriHandler' +import { showConfirmationMessage } from '../shared/utilities/messages' +import globals from '../shared/extensionGlobals' +import { VSCODE_EXTENSION_ID } from '../shared/extensions' + +const localize = nls.loadMessageBundle() + +export function registerLambdaUriHandler() { + async function openFunctionHandler(params: ReturnType) { + const response = await showConfirmationMessage({ + prompt: localize( + 'AWS.lambda.uriUnavailable', + 'The URI you are attempting to access is not in this version of the Toolkit' + ), + confirm: localize('AWS.installLatest', 'Install latest'), + }) + if (response) { + await vscode.commands.executeCommand('extension.open', VSCODE_EXTENSION_ID.awstoolkit) + } + } + + return vscode.Disposable.from( + globals.uriHandler.onPath('/lambda/load-function', openFunctionHandler, parseOpenParams) + ) +} + +export function parseOpenParams(query: SearchParams) { + return { + functionName: query.getOrThrow( + 'functionName', + localize('AWS.lambda.open.missingName', 'A function name must be provided') + ), + region: query.getOrThrow('region', localize('AWS.lambda.open.missingRegion', 'A region must be provided')), + isCfn: query.get('isCfn'), + } +} diff --git a/packages/core/src/test/lambda/uriHandlers.test.ts b/packages/core/src/test/lambda/uriHandlers.test.ts new file mode 100644 index 00000000000..f3e8ae7c368 --- /dev/null +++ b/packages/core/src/test/lambda/uriHandlers.test.ts @@ -0,0 +1,36 @@ +/*! + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ + +import assert from 'assert' +import { SearchParams } from '../../shared/vscode/uriHandler' +import { parseOpenParams } from '../../lambda/uriHandlers' +import { globals } from '../../shared' + +describe('Lambda URI Handler', function () { + describe('load-function', function () { + it('registers for "/lambda/load-function"', function () { + assert.throws(() => globals.uriHandler.onPath('/lambda/load-function', () => {})) + }) + + it('parses parameters', function () { + let query = new SearchParams({ + functionName: 'example', + }) + assert.throws(() => parseOpenParams(query), /A region must be provided/) + query = new SearchParams({ + region: 'example', + }) + assert.throws(() => parseOpenParams(query), /A function name must be provided/) + + const valid = { + functionName: 'example', + region: 'example', + isCfn: 'false', + } + query = new SearchParams(valid) + assert.deepEqual(parseOpenParams(query), valid) + }) + }) +})