DefinitelyTyped/types/akamai-edgeworkers
2020-05-15 12:27:10 -07:00
..
test Update akamai-edgeworkers to include routing, setvariable (#43489) 2020-03-31 10:01:04 -07:00
index.d.ts [akamai-edgeworkers] Update README link (#43995) 2020-04-17 14:21:42 -07:00
OTHER_FILES.txt
README.md [akamai-edgeworkers] Update README link (#43995) 2020-04-17 14:21:42 -07:00
tsconfig.json #no-publishing-comment No more tabs in json documents, trailing newlines for every document 2020-05-15 12:27:10 -07:00
tslint.json

Bindings for the Akamai EdgeWorker API. This allows you to write your EdgeWorkers in TypeScript.

Types are available for the Request and Response objects, as well as the built-in modules.

User Guide

EdgeWorkers are written in ECMAScript6, so you need to set your tsconfig.json to use es6 as the compilation target and module code generator:

{
    "compilerOptions": {
        "module": "es6",
        "target": "es6",
        //...
    }
}

Using the Request and Response Objects

The predefined EdgeWorker callbacks take Request and Response objects as arguments. After you have installed this package, you can create a main.ts with the following stubs:

/// <reference types="akamai-edgeworkers"/>

export function onClientRequest(request: EW.IngressClientRequest) {}
export function onOriginRequest(request: EW.IngressOriginRequest) {}
export function onOriginResponse(request: EW.EgressOriginRequest, response: EW.EgressOriginResponse) {}
export function onClientResponse(request: EW.EgressClientRequest, response: EW.EgressClientResponse) {}

The triple-slashed first line references this package and pulls EW into your namespace.

Using Built-In Modules

Bindings are available for the built-in cookies and url-search-params modules. Once you've added the triple-slash reference to akamai-edgeworkers you can import them normally:

/// <reference types="akamai-edgeworkers"/>

import { Cookies } from 'cookies';

function onClientRequest(request: EW.MutableRequest & EW.HasRespondWith) {
    const cookie = new Cookies(request.getHeader('cookies') || undefined);
    //...
}