From e6c7d05af5cec4070e21123ce8f0635f2d31be4b Mon Sep 17 00:00:00 2001 From: Adrian Machado Date: Tue, 26 Nov 2024 17:57:12 -0800 Subject: [PATCH 1/2] Update outbound policy docs --- policies/custom-code-inbound/doc.md | 4 +- policies/custom-code-outbound/doc.md | 103 ++++++++++++++++++++++++++- 2 files changed, 104 insertions(+), 3 deletions(-) diff --git a/policies/custom-code-inbound/doc.md b/policies/custom-code-inbound/doc.md index e78e1846..c9948766 100644 --- a/policies/custom-code-inbound/doc.md +++ b/policies/custom-code-inbound/doc.md @@ -129,7 +129,7 @@ objects can be as complicated as you like. ```json { "name": "my-first-policy", - "policyType": "custom-code", + "policyType": "custom-code-inbound", "handler": { "export": "default", "module": "$import(./modules/my-first-policy)", @@ -183,7 +183,7 @@ we'll create a dictionary of API keys to `sub` ids. "policies": [ { "name": "my-first-policy", - "policyType": "custom-code", + "policyType": "custom-code-inbound", "handler": { "export": "default", "module": "$import(./modules/my-first-policy)", diff --git a/policies/custom-code-outbound/doc.md b/policies/custom-code-outbound/doc.md index 130b0d89..1f15d62c 100644 --- a/policies/custom-code-outbound/doc.md +++ b/policies/custom-code-outbound/doc.md @@ -64,6 +64,7 @@ We will write an outbound policy that does two things Here's the code: ```ts +// /modules/my-first-policy.ts export default async function ( response: Response, request: ZuploRequest, @@ -104,7 +105,107 @@ that same instance you must first `clone()` to avoid runtime errors such as ::: -### Adding headers +## Wiring up the policy on routes + +Policies are activated by specifying them on routes in the route.oas.json file. +Here's how we could wire up our new route: + +```json +// /config/policies.json +{ + "policies": [ + { + "name": "my-first-policy", + "policyType": "custom-code-outbound", + "handler": { + "export": "default", + "module": "$import(./modules/my-first-policy)" + } + } + ] +} +``` + +```json +// /config/routes.oas.json +{ + ... + "paths": { + "x-zuplo-path": { + "pathMode": "open-api" + }, + "get": { + "summary": "New Route", + "description": "", + "x-zuplo-route": { + "corsPolicy": "none", + "handler": { + "export": "urlForwardHandler", + "module": "$import(@zuplo/runtime)", + "options": { + "baseUrl": "https://getting-started.zuplo.io" + } + }, + "policies": { + "inbound": [], + "outbound": [ + "my-first-policy", + ] + } + }, +} + +} +``` + +### Policy Options + +In your policy configuration, you can specify additional information to +configure your policy on the options property. In the example below we set an +example object with some properties of type string and number. Note these +objects can be as complicated as you like. + +```json +{ + "name": "my-first-policy", + "policyType": "custom-code-outbound", + "handler": { + "export": "default", + "module": "$import(./modules/my-first-policy)", + "options": { + "you": "can", + "specify": "anything", + "here": 0 + } + } +} +``` + +The value of this property will be passed to your policy's handler as the +`options` parameter. Sometimes it's useful to create a type as shown below. + +```ts +type MyPolicyOptionsType = { + you: string; + specify: string; + here: number; +}; +export default async function ( + response: Response, + request: ZuploRequest, + context: ZuploContext, + options: MyPolicyOptionsType, + policyName: string, +) { + // your policy code goes here, and can use the options to perform any + // configuration + context.log.info(options.you); +} +``` + +You can also use the `any` type if you prefer not to create a type. + +## Adding headers Note if you just need to add headers, it more efficient not read the body stream and reuse it, e.g. From 88fe304198eacf3d5b7c77a060bda8d64d401670 Mon Sep 17 00:00:00 2001 From: Adrian Machado Date: Tue, 26 Nov 2024 18:02:20 -0800 Subject: [PATCH 2/2] Fix heading --- policies/custom-code-outbound/doc.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/policies/custom-code-outbound/doc.md b/policies/custom-code-outbound/doc.md index 1f15d62c..6396afa7 100644 --- a/policies/custom-code-outbound/doc.md +++ b/policies/custom-code-outbound/doc.md @@ -158,7 +158,7 @@ Here's how we could wire up our new route: } ``` -### Policy Options +### Custom Policy Options In your policy configuration, you can specify additional information to configure your policy on the options property. In the example below we set an