From a81da4fbd88722a04ba364a9441c71ebeec7c17c Mon Sep 17 00:00:00 2001 From: Ben Blackmore Date: Thu, 18 Jan 2024 13:52:25 +0100 Subject: [PATCH] feat: deep link API # Why To integrate with the OTel GPT, it would be necessary that OTelBin exposes an API to create deep links. # What Add an API under `POST /deep-link` that takes a collector config within the request body and that outputs a deep link within the `Location` response header and within the body (`text/plain` body). --- packages/otelbin/src/app/deep-link/route.tsx | 39 ++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 packages/otelbin/src/app/deep-link/route.tsx diff --git a/packages/otelbin/src/app/deep-link/route.tsx b/packages/otelbin/src/app/deep-link/route.tsx new file mode 100644 index 00000000..ad65fd28 --- /dev/null +++ b/packages/otelbin/src/app/deep-link/route.tsx @@ -0,0 +1,39 @@ +// SPDX-FileCopyrightText: 2024 Dash0 Inc. +// SPDX-License-Identifier: Apache-2.0 + +import { type NextRequest, NextResponse } from "next/server"; +import { serializeUrlState } from "~/lib/urlState/serializeUrlState"; +import { editorBinding } from "~/components/monaco-editor/editorBinding"; + +export const runtime = "edge"; + +export function OPTIONS(request: NextRequest): NextResponse { + return new NextResponse(null, { + status: 200, + headers: getCorsHeaders(request), + }); +} + +export async function POST(request: NextRequest): Promise { + const body = await request.text(); + const emptyParams = new URLSearchParams(); + const pathName = serializeUrlState([editorBinding], "/", emptyParams, emptyParams, { + [editorBinding.name]: body, + }); + const url = new URL(pathName, request.nextUrl).toString(); + return new NextResponse(url, { + status: 200, + headers: { + ...getCorsHeaders(request), + Location: url, + }, + }); +} + +function getCorsHeaders(request: NextRequest) { + return { + "Access-Control-Allow-Origin": request.headers.get("Origin") || "*", + "Access-Control-Allow-Methods": "POST, OPTIONS", + "Access-Control-Allow-Headers": "Content-Type", + }; +}