Azure Static Web AppsのマネージドAPIのRequestとContextに何が入っているのか確認したメモ

## 概要
Azure Static Web Apps の マネージドAPIのAzure FunctionsのHttpTriggerで渡される引数に何が入っているのか知りたかった。
まとまっているドキュメントを見つけられなかったので実際に出力して確認。
どこかにドキュメントがあるなら、だれか教えてくださるとうれしいです。

## 検証コード

```ts
import {
  app,
  HttpRequest,
  HttpResponseInit,
  InvocationContext,
} from '@azure/functions';

export async function httpTrigger1(
  request: HttpRequest,
  context: InvocationContext,
): Promise<HttpResponseInit> {
  context.log(`Http function processed request for url "${request.url}"`);

  const name = request.query.get('name') || (await request.text()) || 'world';
  return {
    body: JSON.stringify({
      msg: `Hello, ${name}!`,
      headers: JSON.stringify([...request.headers.entries()]),
      url: JSON.stringify(request.url),
      method: request.method,
      query: JSON.stringify([...request.query.entries()]),
      context: JSON.stringify(context),
    }),
  };
}

app.http('httpTrigger1', {
  methods: ['GET', 'POST'],
  authLevel: 'anonymous',
  handler: httpTrigger1,
});
```

## REST

```
get https://gentle-smoke-0024c9c00.5.azurestaticapps.net/api/httpTrigger1?test=a
```

## request


パラメータ|コメント|例
--|--|--
method|リクエストのメソッド名|GET
params|パスパラメータ|`{id}`の場合params.id
query|クエリパラメータのMap|query.get('test')で`a`が取得できる 
url|FunctionsのURL。変動することがある。StaticWebAppsのURLとは異なる。|https://494376bb-f9c9-4a2b-917c-817b195c529c.azurewebsites.net/api/httpTrigger1?test=a\
headers|リクエストヘッダのMap|下記表を参照

Header|Value
--|--
authorization|Bearer eyJhbGciOiJI...
client-ip|10.0.0.12:62830
connection|keep-alive
disguised-host|494376bb-f9c9-4a2b-917c-817b195c529c.azurewebsites.net
host|494376bb-f9c9-4a2b-917c-817b195c529c.azurewebsites.net
max-forwards|8
traceparent|00-de77855821e0ccdf9edbb6afc6ecabc4-9099303eccaba7da-00
user-agent|vscode-restclient
was-default-hostname|494376bb-f9c9-4a2b-917c-817b195c529c.azurewebsites.net
x-appservice-proto|https
x-arr-log-id|9bd19712-beee-4f3a-a7ae-2335939a607b
x-arr-ssl|2048
x-forwarded-for|133.226.1.27:63381, 20.205.82.199:46830
x-forwarded-proto|https
x-forwarded-tlsversion|1.3
x-ms-auth-token|Bearer eyJhbGciOiJSUzI1...
x-ms-client-principal-id|anonymous
x-ms-client-principal-idp|azureStaticWebApps
x-ms-original-url|https://gentle-smoke-0024c9c00.5.azurestaticapps.net/api/httpTrigger1?test=a
x-ms-request-id|9bd19712-beee-4f3a-a7ae-2335939a607b
x-original-url|/api/httpTrigger1?test=a
x-site-deployment-id|494376bb-f9c9-4a2b-917c-817b195c529c
x-waws-unencoded-url|/api/httpTrigger1?test=a
### headers
<details><div>

```json
[
    [
        "authorization",
        "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYmYiOjE3MTQzNTQ5ODYsImV4cCI6MTcxNDM1NTI4NiwiaWF0IjoxNzE0MzU0OTg2LCJpc3MiOiJodHRwczovLzQ5NDM3NmJiLWY5YzktNGEyYi05MTdjLTgxN2IxOTVjNTI5Yy5zY20uYXp1cmV3ZWJzaXRlcy5uZXQiLCJhdWQiOiJodHRwczovLzQ5NDM3NmJiLWY5YzktNGEyYi05MTdjLTgxN2IxOTVjNTI5Yy5henVyZXdlYnNpdGVzLm5ldC9henVyZWZ1bmN0aW9ucyJ9.huHEvlvMU07CAf-weHjAR0CboD4JqrO6q_wTJBtL3Ks"
    ],
    [
        "client-ip",
        "10.0.0.12:62830"
    ],
    [
        "connection",
        "keep-alive"
    ],
    [
        "disguised-host",
        "494376bb-f9c9-4a2b-917c-817b195c529c.azurewebsites.net"
    ],
    [
        "host",
        "494376bb-f9c9-4a2b-917c-817b195c529c.azurewebsites.net"
    ],
    [
        "max-forwards",
        "8"
    ],
    [
        "traceparent",
        "00-de77855821e0ccdf9edbb6afc6ecabc4-9099303eccaba7da-00"
    ],
    [
        "user-agent",
        "vscode-restclient"
    ],
    [
        "was-default-hostname",
        "494376bb-f9c9-4a2b-917c-817b195c529c.azurewebsites.net"
    ],
    [
        "x-appservice-proto",
        "https"
    ],
    [
        "x-arr-log-id",
        "9bd19712-beee-4f3a-a7ae-2335939a607b"
    ],
    [
        "x-arr-ssl",
        "2048|256|CN=Microsoft Azure RSA TLS Issuing CA 07, O=Microsoft Corporation, C=US|CN=*.azurewebsites.net, O=Microsoft Corporation, L=Redmond, S=WA, C=US"
    ],
    [
        "x-forwarded-for",
        "133.226.1.27:63381, 20.205.82.199:46830"
    ],
    [
        "x-forwarded-proto",
        "https"
    ],
    [
        "x-forwarded-tlsversion",
        "1.3"
    ],
    [
        "x-ms-auth-token",
        "Bearer eyJhbGciOiJSUzI1NiIsImtpZCI6IkZERDAxRDZEODczRjY1MTIzQzg5QzJEQzFGNkJGNjBCMzJFMjFBNDEiLCJ0eXAiOiJKV1QifQ.eyJwcm4iOiJiblZzYkE9PSIsInN1YiI6ImFub255bW91cyIsImlzcyI6Imh0dHBzOi8vZ2VudGxlLXNtb2tlLTAwMjRjOWMwMC41LmF6dXJlc3RhdGljYXBwcy5uZXQvLmF1dGgiLCJhdWQiOiJodHRwczovLzQ5NDM3NmJiLWY5YzktNGEyYi05MTdjLTgxN2IxOTVjNTI5Yy5henVyZXdlYnNpdGVzLm5ldCIsIm5iZiI6MTcxNDM1NDg2OSwiZXhwIjoxNzE0MzgwMzY5LCJpYXQiOjE3MTQzNTQ4Njl9.FuaFvKiZ0hXrAlxqshuRQelSD3XAQIydTjA4hyyPdR2I4WTnJUID9gRscdHGENhsHml-eplGOmUFVQgVGicuKlXLKS5Xw2QYuo8l0-Anz-B5eSwoULv26kr6ezq5TzRDJB4fNahJbDlE_LBIZzFixiR0-0WsowccFMS-zfATWxZVs-l6MUaLIcSuf_nasfR9SmZp1albicZ6cp7vwfEAuS70AL-5ljnHDkIVCpKjyoORVGAu62F40GYfWxvVhshanSp41DsuPz_9ATlwrW8G4deeNw9Z6nKG_bqKNnSMZbhVugGS4T85fofU41pv-ixWkiC3nRNP3b479yE07Z1P9w"
    ],
    [
        "x-ms-client-principal-id",
        "anonymous"
    ],
    [
        "x-ms-client-principal-idp",
        "azureStaticWebApps"
    ],
    [
        "x-ms-original-url",
        "https://gentle-smoke-0024c9c00.5.azurestaticapps.net/api/httpTrigger1?test=a"
    ],
    [
        "x-ms-request-id",
        "9bd19712-beee-4f3a-a7ae-2335939a607b"
    ],
    [
        "x-original-url",
        "/api/httpTrigger1?test=a"
    ],
    [
        "x-site-deployment-id",
        "494376bb-f9c9-4a2b-917c-817b195c529c"
    ],
    [
        "x-waws-unencoded-url",
        "/api/httpTrigger1?test=a"
    ]
]
```

</div></details>

#### headers authorization

[jwtをデコードすると下記になる](https://jwt.io/)。
リクエストヘッダのauthorizationヘッダは上書きされてしまうので、独自の認証情報を送りたい場合は`custom-authorization`などのようにヘッダ名をずらす工夫が必要になる。
[stack over flow](https://stackoverflow.com/questions/77612816/azure-static-web-app-jwt-token-generation)

```json
{
  "nbf": 1714354986,
  "exp": 1714355286,
  "iat": 1714354986,
  "iss": "https://494376bb-f9c9-4a2b-917c-817b195c529c.scm.azurewebsites.net",
  "aud": "https://494376bb-f9c9-4a2b-917c-817b195c529c.azurewebsites.net/azurefunctions"
}
```
#### headers x-ms-auth-token
jwtをデコードすると下記になる。

```json
{
  "prn": "bnVsbA==",
  "sub": "anonymous",
  "iss": "https://gentle-smoke-0024c9c00.5.azurestaticapps.net/.auth",
  "aud": "https://494376bb-f9c9-4a2b-917c-817b195c529c.azurewebsites.net",
  "nbf": 1714354869,
  "exp": 1714380369,
  "iat": 1714354869
}
```


#### headers x-ms-original-url

Static Web Apps としての固定されたＵＲＬを参照したい場合、このパラメータを使うことになる。

## context

あまり有益な情報は取れなさそう。関数名とりたいときくらい？
`context.log`の利用がメインになりそう。

JSONPath|Value
--|--
$.invocationId|e877dd89-7f8e-45f5-b217-555a5154edb8
$.functionName|httpTrigger1
$.extraInputs|{}
$.extraOutputs|{}
$.traceContext.traceParent|""
$.traceContext.traceState|""
$.traceContext.attributes|{}
$.options.trigger.methods[0]|GET
$.options.trigger.methods[1]|POST
$.options.trigger.authLevel|anonymous
$.options.trigger.type|httpTrigger
$.options.trigger.name|httpTrigger624a61988c
$.options.trigger.direction|in
$.options.return.type|http
$.options.return.name|$return
$.options.return.direction|out
$.options.extraInputs|[]
$.options.extraOutputs|[]

<details><div>

```json
{
    "invocationId": "e877dd89-7f8e-45f5-b217-555a5154edb8",
    "functionName": "httpTrigger1",
    "extraInputs": {},
    "extraOutputs": {},
    "traceContext": {
        "traceParent": "",
        "traceState": "",
        "attributes": {}
    },
    "options": {
        "trigger": {
            "methods": [
                "GET",
                "POST"
            ],
            "authLevel": "anonymous",
            "type": "httpTrigger",
            "name": "httpTrigger624a61988c",
            "direction": "in"
        },
        "return": {
            "type": "http",
            "name": "$return",
            "direction": "out"
        },
        "extraInputs": [],
        "extraOutputs": []
    }
}
```

</div></details>

## 参考

### AzureStaticWebAppsの公式ドキュメント

[Azure Static Web Apps での API サポートの概要](https://learn.microsoft.com/ja-jp/azure/static-web-apps/apis-overview)


### Functionsの公式ドキュメント
[Azure Functions の HTTP トリガー](https://learn.microsoft.com/ja-jp/azure/azure-functions/functions-bindings-http-webhook-trigger?tabs=python-v2%2Cisolated-process%2Cnodejs-v4%2Cfunctionsv2&pivots=programming-language-typescript)