diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 0b68c28c1f5c..327099ea24e1 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -16,7 +16,7 @@ jobs: run: | pip install codespell==2.1.0 # codespell considers some repo name in go.sum are misspelled - git grep --cached -l '' | grep -v go.sum | grep -v pnpm-lock.yaml |xargs codespell --ignore-words=.ignore_words + git grep --cached -l '' | grep -v go.sum | grep -v pnpm-lock.yaml |xargs codespell --ignore-words=.ignore_words --skip="*.ts,*.mts" - name: Merge conflict run: | bash ./utils/check-merge-conflict.sh diff --git a/.licenserc.yaml b/.licenserc.yaml index edb970dc74b4..86edebf9f218 100644 --- a/.licenserc.yaml +++ b/.licenserc.yaml @@ -32,6 +32,7 @@ header: - '**/*.crt' - '**/*.pem' - '**/*.pb.go' + - '**/pnpm-lock.yaml' - '.github/' - 'conf/mime.types' - '**/*.svg' diff --git a/apisix/cli/config.lua b/apisix/cli/config.lua index d7b9e0a7635b..9e8eb3876ba8 100644 --- a/apisix/cli/config.lua +++ b/apisix/cli/config.lua @@ -249,6 +249,7 @@ local _M = { "public-api", "prometheus", "datadog", + "lago", "loki-logger", "elasticsearch-logger", "echo", diff --git a/apisix/plugins/lago.lua b/apisix/plugins/lago.lua new file mode 100644 index 000000000000..3c5b1f1664c5 --- /dev/null +++ b/apisix/plugins/lago.lua @@ -0,0 +1,229 @@ +-- +-- Licensed to the Apache Software Foundation (ASF) under one or more +-- contributor license agreements. See the NOTICE file distributed with +-- this work for additional information regarding copyright ownership. +-- The ASF licenses this file to You under the Apache License, Version 2.0 +-- (the "License"); you may not use this file except in compliance with +-- the License. You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. +-- +local type = type +local pairs = pairs +local math_random = math.random +local ngx = ngx + +local http = require("resty.http") +local bp_manager_mod = require("apisix.utils.batch-processor-manager") +local core = require("apisix.core") +local str_format = core.string.format + +local plugin_name = "lago" +local batch_processor_manager = bp_manager_mod.new("lago logger") + +local schema = { + type = "object", + properties = { + -- core configurations + endpoint_addrs = { + type = "array", + minItems = 1, + items = core.schema.uri_def, + description = "Lago API address, like http://127.0.0.1:3000, " + .. "it supports both self-hosted and cloud. If multiple endpoints are" + .. " configured, the log will be pushed to a randomly determined" + .. " endpoint from the list.", + }, + endpoint_uri = { + type = "string", + minLength = 1, + default = "/api/v1/events/batch", + description = "Lago API endpoint, it needs to be set to the batch send endpoint.", + }, + token = { + type = "string", + description = "Lago API key, create one for your organization on dashboard." + }, + event_transaction_id = { + type = "string", + description = "Event's transaction ID, it is used to identify and de-duplicate" + .. " the event, it supports string templates containing APISIX and" + .. " NGINX variables, like \"req_${request_id}\", which allows you" + .. " to use values returned by upstream services or request-id" + .. " plugin integration", + }, + event_subscription_id = { + type = "string", + description = "Event's subscription ID, which is automatically generated or" + .. " specified by you when you assign the plan to the customer on" + .. " Lago, used to associate API consumption to a customer subscription," + .. " it supports string templates containing APISIX and NGINX variables," + .. " like \"cus_${consumer_name}\", which allows you to use values" + .. " returned by upstream services or APISIX consumer", + }, + event_code = { + type = "string", + description = "Lago billable metric's code for associating an event to a specified" + .. "billable item", + }, + event_properties = { + type = "object", + patternProperties = { + [".*"] = { + type = "string", + minLength = 1, + }, + }, + description = "Event's properties, used to attach information to an event, this" + .. " allows you to send certain information on a event to Lago, such" + .. " as sending HTTP status to take a failed request off the bill, or" + .. " sending the AI token consumption in the response body for accurate" + .. " billing, its keys are fixed strings and its values can be string" + .. " templates containing APISIX and NGINX variables, like \"${status}\"" + }, + + -- connection layer configurations + ssl_verify = {type = "boolean", default = true}, + timeout = { + type = "integer", + minimum = 1, + maximum = 60000, + default = 3000, + description = "timeout in milliseconds", + }, + keepalive = {type = "boolean", default = true}, + keepalive_timeout = { + type = "integer", + minimum = 1000, + default = 60000, + description = "keepalive timeout in milliseconds", + }, + keepalive_pool = {type = "integer", minimum = 1, default = 5}, + }, + required = {"endpoint_addrs", "token", "event_transaction_id", "event_subscription_id", + "event_code"}, + encrypt_fields = {"token"}, +} +schema = batch_processor_manager:wrap_schema(schema) + +-- According to https://getlago.com/docs/api-reference/events/batch, the maximum batch size is 100, +-- so we have to override the default batch size to make it work out of the box,the plugin does +-- not set a maximum limit, so if Lago relaxes the limit, then user can modify it +-- to a larger batch size +-- This does not affect other plugins, schema is appended after deep copy +schema.properties.batch_max_size.default = 100 + + +local _M = { + version = 0.1, + priority = 415, + name = plugin_name, + schema = schema, +} + + +function _M.check_schema(conf, schema_type) + local check = {"endpoint_addrs"} + core.utils.check_https(check, conf, plugin_name) + core.utils.check_tls_bool({"ssl_verify"}, conf, plugin_name) + + return core.schema.check(schema, conf) +end + + +local function send_http_data(conf, data) + local body, err = core.json.encode(data) + if not body then + return false, str_format("failed to encode json: %s", err) + end + local params = { + headers = { + ["Content-Type"] = "application/json", + ["Authorization"] = "Bearer " .. conf.token, + }, + keepalive = conf.keepalive, + ssl_verify = conf.ssl_verify, + method = "POST", + body = body, + } + + if conf.keepalive then + params.keepalive_timeout = conf.keepalive_timeout + params.keepalive_pool = conf.keepalive_pool + end + + local httpc, err = http.new() + if not httpc then + return false, str_format("create http client error: %s", err) + end + httpc:set_timeout(conf.timeout) + + -- select an random endpoint and build URL + local endpoint_url = conf.endpoint_addrs[math_random(#conf.endpoint_addrs)]..conf.endpoint_uri + local res, err = httpc:request_uri(endpoint_url, params) + if not res then + return false, err + end + + if res.status >= 300 then + return false, str_format("lago api returned status: %d, body: %s", + res.status, res.body or "") + end + + return true +end + + +function _M.log(conf, ctx) + -- build usage event + local event_transaction_id, err = core.utils.resolve_var(conf.event_transaction_id, ctx.var) + if err then + core.log.error("failed to resolve event_transaction_id, event dropped: ", err) + return + end + + local event_subscription_id, err = core.utils.resolve_var(conf.event_subscription_id, ctx.var) + if err then + core.log.error("failed to resolve event_subscription_id, event dropped: ", err) + return + end + + local entry = { + transaction_id = event_transaction_id, + external_subscription_id = event_subscription_id, + code = conf.event_code, + timestamp = ngx.req.start_time(), + } + + if conf.event_properties and type(conf.event_properties) == "table" then + entry.properties = core.table.deepcopy(conf.event_properties) + for key, value in pairs(entry.properties) do + local new_val, err, n_resolved = core.utils.resolve_var(value, ctx.var) + if not err and n_resolved > 0 then + entry.properties[key] = new_val + end + end + end + + if batch_processor_manager:add_entry(conf, entry) then + return + end + + -- generate a function to be executed by the batch processor + local func = function(entries) + return send_http_data(conf, { + events = entries, + }) + end + + batch_processor_manager:add_entry_to_new_processor(conf, entry, ctx, func) +end + + +return _M diff --git a/conf/config.yaml.example b/conf/config.yaml.example index 176f0f510bd2..a31785bff9b3 100644 --- a/conf/config.yaml.example +++ b/conf/config.yaml.example @@ -511,6 +511,7 @@ plugins: # plugin list (sorted by priority) - public-api # priority: 501 - prometheus # priority: 500 - datadog # priority: 495 + - lago # priority: 415 - loki-logger # priority: 414 - elasticsearch-logger # priority: 413 - echo # priority: 412 diff --git a/docs/en/latest/config.json b/docs/en/latest/config.json index bdbdf778596d..53778bcfcd76 100644 --- a/docs/en/latest/config.json +++ b/docs/en/latest/config.json @@ -212,7 +212,8 @@ "plugins/loggly", "plugins/elasticsearch-logger", "plugins/tencent-cloud-cls", - "plugins/loki-logger" + "plugins/loki-logger", + "plugins/lago" ] } ] diff --git a/docs/en/latest/plugins/lago.md b/docs/en/latest/plugins/lago.md new file mode 100644 index 000000000000..ab78f2627680 --- /dev/null +++ b/docs/en/latest/plugins/lago.md @@ -0,0 +1,259 @@ +--- +title: lago +keywords: + - Apache APISIX + - API Gateway + - Plugin + - lago + - monetization + - github.com/getlago/lago +description: The lago plugin reports usage to a Lago instance, which allows users to integrate Lago with APISIX for API monetization. +--- + + + + + + + +## Description + +The `lago` plugin pushes requests and responses to [Lago Self-hosted](https://github.com/getlago/lago) and [Lago Cloud](https://getlago.com) via the Lago REST API. the plugin allows you to use it with a variety of APISIX built-in features, such as the APISIX consumer and the request-id plugin. + +This allows for API monetization or let APISIX to be an AI gateway for AI tokens billing scenarios. + +:::disclaimer + +Lago owns its trademarks and controls its commercial products and open source projects. + +The [https://github.com/getlago/lago](https://github.com/getlago/lago) project uses the `AGPL-3.0` license instead of the `Apache-2.0` license that is the same as Apache APISIX. As a user, you will need to evaluate for yourself whether it is applicable to your business to use the project in a compliant way or to obtain another type of license from Lago. Apache APISIX community does not endorse it. + +The plugin does not contain any proprietary code or SDKs from Lago, it is contributed by contributors to Apache APISIX and licensed under the `Apache-2.0` license, which is in line with any other part of APISIX and you don't need to worry about its compliance. + +::: + +When enabled, the plugin will collect information from the request context (e.g. event code, transaction ID, associated subscription ID) as configured and serialize them into [Event JSON objects](https://getlago.com/docs/api-reference/events/event-object) as required by Lago. They will be added to the buffer and sent to Lago in batches of up to 100. This batch size is a [requirement](https://getlago.com/docs/api-reference/events/batch) from Lago. If you want to modify it, see [batch processor](../batch-processor.md) for more details. + +## Attributes + +| Name | Type | Required | Default | Valid values | Description | +|---|---|---|---|---|---| +| endpoint_addrs | array[string] | True | | | Lago API address, like `http://127.0.0.1:3000`, it supports both self-hosted and cloud. If multiple endpoints are configured, the log will be pushed to a randomly determined endpoint from the list. | +| endpoint_uri | string | False | /api/v1/events/batch | | Lago API endpoint, it needs to be set to the batch send endpoint. | +| token | string | True | | | Lago API key, create one for your organization on Lago dashboard. | +| event_transaction_id | string | True | | | Event's transaction ID, it is used to identify and de-duplicate the event, it supports string templates containing APISIX and NGINX variables, like `req_${request_id}`, which allows you to use values returned by upstream services or request-id plugin integration | +| event_subscription_id | string | True | | | Event's subscription ID, which is automatically generated or specified by you when you assign the plan to the customer on Lago, used to associate API consumption to a customer subscription, it supports string templates containing APISIX and NGINX variables, like `cus_${consumer_name}`, which allows you to use values returned by upstream services or APISIX consumer | +| event_code | string | True | | | Lago billable metric's code for associating an event to a specified billable item | +| event_properties | object | False | | | Event's properties, used to attach information to an event, this allows you to send certain information on a event to Lago, such as sending HTTP status to take a failed request off the bill, or sending the AI token consumption in the response body for accurate billing, its keys are fixed strings and its values can be string templates containing APISIX and NGINX variables, like `${status}` | +| ssl_verify | boolean | False | true | | If true, verify Lago's SSL certificates. | +| timeout | integer | False | 3000 | [1, 60000] | Timeout for the Lago service HTTP call in milliseconds. | +| keepalive | boolean | False | true | | If true, keep the connection alive for multiple requests. | +| keepalive_timeout | integer | False | 60000 | >=1000 | Keepalive timeout in milliseconds. | +| keepalive_pool | integer | False | 5 | >=1 | Maximum number of connections in the connection pool. | + +This Plugin supports using batch processors to aggregate and process events in a batch. This avoids the need for frequently submitting the data. The batch processor submits data every `5` seconds or when the data in the queue reaches `1000`. See [Batch Processor](../batch-processor.md#configuration) for more information or setting your custom configuration. + +## Examples + +The examples below demonstrate how you can configure `lago` Plugin for typical scenario. + +To follow along the examples, start a Lago instance. Refer to [https://github.com/getlago/lago](https://github.com/getlago/lago) or use Lago cloud. + +Follow these brief steps to configure the Lago: + +1. Get the Lago API Key (also known as `token`), from the Developer page of the Lago dashboard. +2. Next, create the Billable metric used by APISIX, assuming its code is `test`. Set the `Aggregation type` to `Count`; and add a filter with a key of `tier` whose value contains `expensive` to allow us to distinguish between API values, which will be demonstrated later. +3. Create a Plan and add the created metric to it. Its code doesn't matter, you can specify as much as you like. In the `Usage-based charges` section, add the billable metric you just created as a `Metered charge` item. Specify the default price as `1$`. Add a filter, use `tier: expensive` to perform the filtering, and specify its price as `10$`. +4. Select an existing or create a new consumer to assign the Plan you just created to it. You need to specify a `Subscription external ID` (or you can have Lago generate it), which will be used as the APISIX consumer username. + +Next we need to configure the APISIX for demonstrates. + +:::note + +You can fetch the `admin_key` from `config.yaml` and save to an environment variable with the following command: + +```bash +admin_key=$(yq '.deployment.admin.admin_key[0].key' conf/config.yaml | sed 's/"//g') +``` + +::: + +### Report API call usage + +The following example demonstrates how you can configure the `lago` Plugin on a Route to measuring API call usage. + +Create a Route 1 with the `lago`, `request-id`, `key-auth` Plugin and configure it: + +```shell +curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \ + -H "X-API-KEY: ${admin_key}" \ + -d '{ + "id": "lago-route-1", + "uri": "/get", + "plugins": { + "request-id": { + "include_in_response": true + }, + "key-auth": {}, + "lago": { + "endpoint_addrs": ["http://12.0.0.1:3000"], + "token": "", + "event_transaction_id": "${http_x_request_id}", + "event_subscription_id": "${http_x_consumer_username}", + "event_code": "test" + } + }, + "upstream": { + "nodes": { + "httpbin.org:80": 1 + }, + "type": "roundrobin" + } + }' +``` + +Create Route 2: + +```shell +curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \ + -H "X-API-KEY: ${admin_key}" \ + -d '{ + "id": "lago-route-2", + "uri": "/anything", + "plugins": { + "request-id": { + "include_in_response": true + }, + "key-auth": {}, + "lago": { + "endpoint_addrs": ["http://12.0.0.1:3000"], + "token": "", + "event_transaction_id": "${http_x_request_id}", + "event_subscription_id": "${http_x_consumer_username}", + "event_code": "test", + "event_properties": { + "tier": "expensive" + } + } + }, + "upstream": { + "nodes": { + "httpbin.org:80": 1 + }, + "type": "roundrobin" + } + }' +``` + +Creating Consumer: + +```shell +curl "http://127.0.0.1:9180/apisix/admin/consumers" -X PUT \ + -H "X-API-KEY: ${admin_key}" \ + -d '{ + "username": "", + "plugins": { + "key-auth": { + "key": "demo" + } + } + }' +``` + +Send three requests to two separate routes: + +```shell +curl "http://127.0.0.1:9080/get" +curl "http://127.0.0.1:9080/get" +curl "http://127.0.0.1:9080/get" +curl "http://127.0.0.1:9080/anything" +curl "http://127.0.0.1:9080/anything" +curl "http://127.0.0.1:9080/anything" +``` + +You should receive `HTTP/1.1 200 OK` responses for all requests. + +Wait a few seconds, go to the `Developer` page in the Lago dashboard, look at `Events` and you will see 6 event entries sent by APISIX. + +If the self-hosted instance's event worker is configured correctly (or if you're using Lago Cloud), you can also see the total amount consumed in real time in the consumer's subscription usage, which should be `3 * 1$ + 3 * 10$ = 33$` according to our demo use case. + +## FAQ + +### What's this for? + +When you make an effort to monetize your API, it's hard to find a ready-made, low-cost solution, so you may have to build your own billing stack, which is complicated. + +This plugin allows you to use APISIX to handle API proxies and use Lago as a billing stack through direct integration with Lago, and both the APISIX open source project and Lago will be part of your portfolio, which is a huge time saver. + +Every API call results in a Lago event, which allows you to bill users for real usage, i.e. pay-as-you-go, and thanks to our built-in transaction ID (request ID) support, you can simply implement API call logging and troubleshooting for your customers. + +In addition to typical API monetization scenarios, APISIX can also do AI tokens-based billing when it is acting as an AI gateway, where each Lago event generated by an API request includes exactly how many tokens were consumed, to allow you to charge the user for a fine-grained per-tokens usage. + +### Is it flexible? + +Of course, the fact that we make transaction ID, subscription ID as a configuration item and allow you to use APISIX and NGINX variables in it means that it's simple to integrate the plugin with any existing or your own authentication and internal services. + +- Use custom authentication: as long as the Lago subscription ID represented by the user ID is registered as an APISIX variable, it will be available from there, so custom authentication is completely possible! +- Integration with internal services: sometimes you may not need the APISIX built-in `request-id` plugin, it doesn't matter, you can have your internal service (APISIX upstream) generate it and include it in the HTTP response header so that you can access it by way of an NGINX variable in the transaction ID. + +Support for event properties is provided, this allows you to set special values on specific APIs, for example if your service has 100 APIs, and you want to enable general billing on all of them and special configure them on a few specific APIs to apply different pricing, this will work, as we did in the demo above. + +### Which Lago versions does it work with? + +When we first developed the Lago plugin, it was released to `1.17.0`, which we used for integration, so it works at least with `1.17.0`. + +Technically, we use the Lago batch event API to submit events in batches, and APISIX will only use this API, so as long as Lago doesn't make any disruptive changes to this API, APISIX will be able to integrate with it. + +Here's an [archive page](https://web.archive.org/web/20250516073803/https://getlago.com/docs/api-reference/events/batch) of the API documentation, which allows you to check the differences between the API at the time of our integration and the latest API. + +If the latest API changes, then you can submit an issue to inform the APISIX maintainers that this may require some changes. + +### Events are not pushed properly + +Look at `error.log` for such a log. + +```text +2023/04/30 13:45:46 [error] 19381#19381: *1075673 [lua] batch-processor.lua:95: Batch Processor[lago logger] failed to process entries: lago api returned status: 400, body: , context: ngx.timer, client: 127.0.0.1, server: 0.0.0.0:9080 +``` + +The error can be diagnosed based on the error code in the `failed to process entries: lago api returned status: 400, body: ` and the response body of the lago server. + +### Reliability of reporting + +The plugin may encounter a network problem that prevents the node where the gateway is located from communicating with the Lago API, in which case APISIX will discard the batch according to the [batch processor](../batch-processor.md) configuration, the batch will be discarded if the specified number of retries are made and the dosage still cannot be sent. + +Discarded events are permanently lost, so it is recommended that you use this plugin in conjunction with other logging mechanisms and perform event replay after Lago is unavailable causing data to be discarded to ensure that all logs are correctly sent to Lago. + +### Will the event duplicate? + +While APISIX performs retries based on the [batch processor](../batch-processor.md) configuration, you don't need to worry about duplicate events being reported to Lago. + +The `event_transcation_id` and `timestamp` are generated and logged after the request is processed on the APISIX side, and Lago de-duplicates the event based on them. +So even if a retry is triggered because the network causes Lago to send a `success` response that is not received by APISIX, the event is still not duplicated on Lago. + +### Performance Impacts + +The plugin is logically simple and reliable, it just builds a Lago event object for each request, buffers and sends them in bulk. The logic is not coupled to the request proxy path, so this does not cause latency to rise for requests going through the gateway. + +Technically, the logic is executed in the NGINX log phase and [batch processor](../batch-processor.md) timer, so this does not affect the request itself. + +### Resource overhead + +For the reasons mentioned above in performance impact section, the plugin does not result in a significant resource overhead, it only consumes a few worker memory to buffer events for batching. diff --git a/t/admin/plugins.t b/t/admin/plugins.t index bbacad6ab3a1..d1683b46bbca 100644 --- a/t/admin/plugins.t +++ b/t/admin/plugins.t @@ -124,6 +124,7 @@ http-dubbo public-api prometheus datadog +lago loki-logger elasticsearch-logger echo diff --git a/t/jest.config.ts b/t/jest.config.ts index b72e3e333257..8fa91d0fd22a 100644 --- a/t/jest.config.ts +++ b/t/jest.config.ts @@ -14,15 +14,18 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -import type {Config} from 'jest'; +import type { Config } from 'jest'; const config: Config = { - coverageProvider: "v8", - testEnvironment: "node", + coverageProvider: 'v8', + testEnvironment: 'node', + testRegex: '(/__tests__/.*|(\\.|/)(spec|test))\\.(ts|mts)$', transform: { - "^.+\.tsx?$": ["ts-jest",{}], + '^.+\\.ts$': ['ts-jest', { useESM: false }], + '^.+\\.mts$': ['ts-jest', { useESM: true, tsconfig: 'tsconfig.esm.json' }], }, + extensionsToTreatAsEsm: ['.mts'], + moduleFileExtensions: ['ts', 'mts', 'js'], }; export default config; diff --git a/t/package.json b/t/package.json index 48c5cdaa6e2a..9d42d44a8996 100644 --- a/t/package.json +++ b/t/package.json @@ -1,14 +1,22 @@ { "name": "apisix-test-suite", "private": true, + "type": "module", "scripts": { - "test": "jest" + "test": "NODE_OPTIONS=--experimental-vm-modules jest" }, "devDependencies": { + "@jest/globals": "^29.7.0", + "@trivago/prettier-plugin-sort-imports": "^5.2.2", "@types/jest": "29.5.14", "@types/node": "22.14.1", "axios": "^1.9.0", + "docker-compose": "^1.2.0", + "graphql": "^16.11.0", + "graphql-request": "^7.1.2", "jest": "29.7.0", + "lago-javascript-client": "^1.26.0", + "simple-git": "^3.27.0", "ts-jest": "29.3.2", "ts-node": "10.9.2", "yaml": "^2.7.1" diff --git a/t/plugin/lago.spec.mts b/t/plugin/lago.spec.mts new file mode 100644 index 000000000000..36833cdb866e --- /dev/null +++ b/t/plugin/lago.spec.mts @@ -0,0 +1,352 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import { generateKeyPair } from 'node:crypto'; +import { existsSync } from 'node:fs'; +import { readFile, rm, writeFile } from 'node:fs/promises'; +import { promisify } from 'node:util'; + +import { afterAll, beforeAll, describe, expect, it } from '@jest/globals'; +import axios from 'axios'; +import * as compose from 'docker-compose'; +import { gql, request } from 'graphql-request'; +import { Api as LagoApi, Client as LagoClient } from 'lago-javascript-client'; +import simpleGit from 'simple-git'; +import * as YAML from 'yaml'; + +import { request as requestAdminAPI } from '../ts/admin_api'; +import { wait } from '../ts/utils'; + +const LAGO_VERSION = 'v1.27.0'; +const LAGO_PATH = '/tmp/lago'; +const LAGO_FRONT_PORT = 59999; +const LAGO_API_PORT = 30699; +const LAGO_API_URL = `http://127.0.0.1:${LAGO_API_PORT}`; +const LAGO_API_BASEURL = `http://127.0.0.1:${LAGO_API_PORT}/api/v1`; +const LAGO_API_GRAPHQL_ENDPOINT = `${LAGO_API_URL}/graphql`; +const LAGO_BILLABLE_METRIC_CODE = 'test'; +const LAGO_EXTERNAL_SUBSCRIPTION_ID = 'jack_test'; + +// The project uses AGPLv3, so we can't store the docker compose file it uses in our repository and download it during testing. +const downloadComposeFile = async () => + simpleGit().clone('https://github.com/getlago/lago', LAGO_PATH, { + '--depth': '1', + '--branch': LAGO_VERSION, + }); + +const launchLago = async () => { + // patch docker-compose.yml to disable useless port + const composeFilePath = `${LAGO_PATH}/docker-compose.yml`; + const composeFile = YAML.parse(await readFile(composeFilePath, 'utf8')); + delete composeFile.services.front; // front-end is not needed for tests + delete composeFile.services['api-clock']; // clock is not needed for tests + delete composeFile.services['api-worker']; // worker is not needed for tests + delete composeFile.services['pdf']; // pdf is not needed for tests + delete composeFile.services.redis.ports; // prevent port conflict + delete composeFile.services.db.ports; // prevent port conflict + await writeFile(composeFilePath, YAML.stringify(composeFile), 'utf8'); + + // launch services + const { privateKey } = await promisify(generateKeyPair)('rsa', { + modulusLength: 2048, + publicKeyEncoding: { type: 'pkcs1', format: 'pem' }, + privateKeyEncoding: { type: 'pkcs1', format: 'pem' }, + }); + const composeOpts: compose.IDockerComposeOptions = { + cwd: LAGO_PATH, + log: true, + env: { + LAGO_RSA_PRIVATE_KEY: Buffer.from(privateKey).toString('base64'), + FRONT_PORT: `${LAGO_FRONT_PORT}`, // avoiding conflicts, tests do not require a front-end + API_PORT: `${LAGO_API_PORT}`, + LAGO_FRONT_URL: `http://127.0.0.1:${LAGO_FRONT_PORT}`, + LAGO_API_URL, + }, + }; + + await compose.createAll(composeOpts); + await compose.upOne('api', composeOpts); + await compose.exec('api', 'rails db:create', composeOpts); + await compose.exec('api', 'rails db:migrate', composeOpts); + await compose.upAll(composeOpts); +}; + +const provisionLago = async () => { + // sign up + const { registerUser } = await request<{ + registerUser: { token: string; user: { organizations: { id: string } } }; + }>( + LAGO_API_GRAPHQL_ENDPOINT, + gql` + mutation signup($input: RegisterUserInput!) { + registerUser(input: $input) { + token + user { + id + organizations { + id + } + } + } + } + `, + { + input: { + email: 'test@test.com', + password: 'Admin000!', + organizationName: 'test', + }, + }, + ); + + const webToken = registerUser.token; + const organizationId = registerUser.user.organizations[0].id; + const requestHeaders = { + Authorization: `Bearer ${webToken}`, + 'X-Lago-Organization': organizationId, + }; + + // list api keys + const { apiKeys } = await request<{ + apiKeys: { collection: { id: string }[] }; + }>( + LAGO_API_GRAPHQL_ENDPOINT, + gql` + query getApiKeys { + apiKeys(page: 1, limit: 20) { + collection { + id + } + } + } + `, + {}, + requestHeaders, + ); + + // get first api key + const { apiKey } = await request<{ apiKey: { value: string } }>( + LAGO_API_GRAPHQL_ENDPOINT, + gql` + query getApiKeyValue($id: ID!) { + apiKey(id: $id) { + id + value + } + } + `, + { id: apiKeys.collection[0].id }, + requestHeaders, + ); + + const lagoClient = LagoClient(apiKey.value, { baseUrl: LAGO_API_BASEURL }); + + // create billable metric + const { data: billableMetric } = + await lagoClient.billableMetrics.createBillableMetric({ + billable_metric: { + name: LAGO_BILLABLE_METRIC_CODE, + code: LAGO_BILLABLE_METRIC_CODE, + aggregation_type: 'count_agg', + filters: [ + { + key: 'tier', + values: ['normal', 'expensive'], + }, + ], + }, + }); + + // create plan + const { data: plan } = await lagoClient.plans.createPlan({ + plan: { + name: 'test', + code: 'test', + interval: 'monthly', + amount_cents: 0, + amount_currency: 'USD', + pay_in_advance: false, + charges: [ + { + billable_metric_id: billableMetric.billable_metric.lago_id, + charge_model: 'standard', + pay_in_advance: false, + properties: { amount: '1' }, + filters: [ + { + properties: { amount: '10' }, + values: { tier: ['expensive'] }, + }, + ], + }, + ], + }, + }); + + // create customer + const external_customer_id = 'jack'; + const { data: customer } = await lagoClient.customers.createCustomer({ + customer: { + external_id: external_customer_id, + name: 'Jack', + currency: 'USD', + }, + }); + + // assign plan to customer + await lagoClient.subscriptions.createSubscription({ + subscription: { + external_customer_id: customer.customer.external_id, + plan_code: plan.plan.code, + external_id: LAGO_EXTERNAL_SUBSCRIPTION_ID, + }, + }); + + return { apiKey: apiKey.value, client: lagoClient }; +}; + +describe('Plugin - Lago', () => { + const JACK_USERNAME = 'jack_test'; + const client = axios.create({ baseURL: 'http://127.0.0.1:1984' }); + + let restAPIKey: string; + let lagoClient: LagoApi; // prettier-ignore + + // set up + beforeAll(async () => { + if (existsSync(LAGO_PATH)) await rm(LAGO_PATH, { recursive: true }); + await downloadComposeFile(); + await launchLago(); + let res = await provisionLago(); + restAPIKey = res.apiKey; + lagoClient = res.client; + }, 120 * 1000); + + // clean up + afterAll(async () => { + await compose.downAll({ + cwd: LAGO_PATH, + commandOptions: ['--volumes'], + }); + await rm(LAGO_PATH, { recursive: true }); + }, 30 * 1000); + + it('should create route', async () => { + await expect( + requestAdminAPI('/apisix/admin/routes/1', 'PUT', { + uri: '/hello', + upstream: { + nodes: { + '127.0.0.1:1980': 1, + }, + type: 'roundrobin', + }, + plugins: { + 'request-id': { include_in_response: true }, // for transaction_id + 'key-auth': {}, // for subscription_id + lago: { + endpoint_addrs: [LAGO_API_URL], + token: restAPIKey, + event_transaction_id: '${http_x_request_id}', + event_subscription_id: '${http_x_consumer_username}', + event_code: 'test', + batch_max_size: 1, // does not buffered usage reports + }, + }, + }), + ).resolves.not.toThrow(); + + await expect( + requestAdminAPI('/apisix/admin/routes/2', 'PUT', { + uri: '/hello1', + upstream: { + nodes: { + '127.0.0.1:1980': 1, + }, + type: 'roundrobin', + }, + plugins: { + 'request-id': { include_in_response: true }, + 'key-auth': {}, + lago: { + endpoint_addrs: [LAGO_API_URL], + token: restAPIKey, + event_transaction_id: '${http_x_request_id}', + event_subscription_id: '${http_x_consumer_username}', + event_code: 'test', + event_properties: { tier: 'expensive' }, + batch_max_size: 1, + }, + }, + }), + ).resolves.not.toThrow(); + }); + + it('should create consumer', async () => + expect( + requestAdminAPI(`/apisix/admin/consumers/${JACK_USERNAME}`, 'PUT', { + username: JACK_USERNAME, + plugins: { + 'key-auth': { key: JACK_USERNAME }, + }, + }), + ).resolves.not.toThrow()); + + it('call API (without key)', async () => { + const res = await client.get('/hello', { validateStatus: () => true }); + expect(res.status).toEqual(401); + }); + + it('call normal API', async () => { + for (let i = 0; i < 3; i++) { + await expect( + client.get('/hello', { headers: { apikey: JACK_USERNAME } }), + ).resolves.not.toThrow(); + } + await wait(500); + }); + + it('check Lago events (normal API)', async () => { + const { data } = await lagoClient.events.findAllEvents({ + external_subscription_id: LAGO_EXTERNAL_SUBSCRIPTION_ID, + }); + + expect(data.events).toHaveLength(3); + expect(data.events[0].code).toEqual(LAGO_BILLABLE_METRIC_CODE); + }); + + let expensiveStartAt: Date; + it('call expensive API', async () => { + expensiveStartAt = new Date(); + for (let i = 0; i < 3; i++) { + await expect( + client.get('/hello1', { headers: { apikey: JACK_USERNAME } }), + ).resolves.not.toThrow(); + } + await wait(500); + }); + + it('check Lago events (expensive API)', async () => { + const { data } = await lagoClient.events.findAllEvents({ + external_subscription_id: LAGO_EXTERNAL_SUBSCRIPTION_ID, + timestamp_from: expensiveStartAt.toISOString(), + }); + + expect(data.events).toHaveLength(3); + expect(data.events[0].code).toEqual(LAGO_BILLABLE_METRIC_CODE); + expect(data.events[1].properties).toEqual({ tier: 'expensive' }); + }); +}); diff --git a/t/plugin/lago.t b/t/plugin/lago.t new file mode 100644 index 000000000000..7e1c6409348d --- /dev/null +++ b/t/plugin/lago.t @@ -0,0 +1,77 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +use t::APISIX 'no_plan'; + +repeat_each(1); +no_long_string(); +no_root_location(); + +add_block_preprocessor(sub { + my ($block) = @_; + + if (!defined $block->request) { + $block->set_value("request", "GET /t"); + } +}); + +run_tests(); + +__DATA__ + +=== TEST 1: sanity +--- config + location /t { + content_by_lua_block { + local test_cases = { + {endpoint_addrs = {"http://127.0.0.1:3000"}, token = "token", event_transaction_id = "tid", event_subscription_id = "sid", event_code = "code"}, + {endpoint_addrs = "http://127.0.0.1:3000", token = "token", event_transaction_id = "tid", event_subscription_id = "sid", event_code = "code"}, + {endpoint_addrs = {}, token = "token", event_transaction_id = "tid", event_subscription_id = "sid", event_code = "code"}, + {endpoint_addrs = {"http://127.0.0.1:3000"}, endpoint_uri = "/test", token = "token", event_transaction_id = "tid", event_subscription_id = "sid", event_code = "code"}, + {endpoint_addrs = {"http://127.0.0.1:3000"}, endpoint_uri = 1234, token = "token", event_transaction_id = "tid", event_subscription_id = "sid", event_code = "code"}, + {endpoint_addrs = {"http://127.0.0.1:3000"}, token = 1234, event_transaction_id = "tid", event_subscription_id = "sid", event_code = "code"}, + {endpoint_addrs = {"http://127.0.0.1:3000"}, token = "token", event_transaction_id = "tid", event_subscription_id = "sid", event_code = "code", event_properties = {key = "value"}}, + {endpoint_addrs = {"http://127.0.0.1:3000"}, token = "token", event_transaction_id = "tid", event_subscription_id = "sid", event_code = "code", event_properties = {1,2,3}}, + } + local plugin = require("apisix.plugins.lago") + + for _, case in ipairs(test_cases) do + local ok, err = plugin.check_schema(case) + ngx.say(ok and "done" or err) + end + } + } +--- response_body +done +property "endpoint_addrs" validation failed: wrong type: expected array, got string +property "endpoint_addrs" validation failed: expect array to have at least 1 items +done +property "endpoint_uri" validation failed: wrong type: expected string, got number +property "token" validation failed: wrong type: expected string, got number +done +property "event_properties" validation failed: wrong type: expected object, got table + + + +=== TEST 2: test +--- timeout: 300 +--- max_size: 2048000 +--- exec +cd t && pnpm test plugin/lago.spec.mts 2>&1 +--- no_error_log +failed to execute the script with status +--- response_body eval +qr/PASS plugin\/lago.spec.mts/ diff --git a/t/pnpm-lock.yaml b/t/pnpm-lock.yaml index 9d670ec78ca7..0a636dcd6211 100644 --- a/t/pnpm-lock.yaml +++ b/t/pnpm-lock.yaml @@ -1,20 +1,3 @@ -# -# Licensed to the Apache Software Foundation (ASF) under one or more -# contributor license agreements. See the NOTICE file distributed with -# this work for additional information regarding copyright ownership. -# The ASF licenses this file to You under the Apache License, Version 2.0 -# (the "License"); you may not use this file except in compliance with -# the License. You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# - lockfileVersion: '9.0' settings: @@ -25,6 +8,12 @@ importers: .: devDependencies: + '@jest/globals': + specifier: ^29.7.0 + version: 29.7.0 + '@trivago/prettier-plugin-sort-imports': + specifier: ^5.2.2 + version: 5.2.2(prettier@3.5.3) '@types/jest': specifier: 29.5.14 version: 29.5.14 @@ -34,9 +23,24 @@ importers: axios: specifier: ^1.9.0 version: 1.9.0 + docker-compose: + specifier: ^1.2.0 + version: 1.2.0 + graphql: + specifier: ^16.11.0 + version: 16.11.0 + graphql-request: + specifier: ^7.1.2 + version: 7.1.2(graphql@16.11.0) jest: specifier: 29.7.0 version: 29.7.0(@types/node@22.14.1)(ts-node@10.9.2(@types/node@22.14.1)(typescript@5.8.3)) + lago-javascript-client: + specifier: ^1.26.0 + version: 1.26.0 + simple-git: + specifier: ^3.27.0 + version: 3.27.0 ts-jest: specifier: 29.3.2 version: 29.3.2(@babel/core@7.26.10)(@jest/transform@29.7.0)(@jest/types@29.6.3)(babel-jest@29.7.0(@babel/core@7.26.10))(jest@29.7.0(@types/node@22.14.1)(ts-node@10.9.2(@types/node@22.14.1)(typescript@5.8.3)))(typescript@5.8.3) @@ -218,6 +222,11 @@ packages: resolution: {integrity: sha512-IchNf6dN4tHoMFIn/7OE8LWZ19Y6q/67Bmf6vnGREv8RSbBVb9LPJxEcnwrcwX6ixSvaiGoomAUvu4YSxXrVgw==} engines: {node: '>=12'} + '@graphql-typed-document-node/core@3.2.0': + resolution: {integrity: sha512-mB9oAsNCm9aM3/SOv4YtBMqZbYj10R7dkq8byBqxGY/ncFwhf2oQzMV+LCRlWoDSEBJ3COiR1yeDvMtsoOsuFQ==} + peerDependencies: + graphql: ^0.8.0 || ^0.9.0 || ^0.10.0 || ^0.11.0 || ^0.12.0 || ^0.13.0 || ^14.0.0 || ^15.0.0 || ^16.0.0 || ^17.0.0 + '@istanbuljs/load-nyc-config@1.1.0': resolution: {integrity: sha512-VjeHSlIzpv/NyD3N0YuHfXOPDIixcA1q2ZV98wsMqcYlPmv2n3Yb2lYP9XMElnaFVXg5A7YLTeLu6V84uQDjmQ==} engines: {node: '>=8'} @@ -313,6 +322,12 @@ packages: '@jridgewell/trace-mapping@0.3.9': resolution: {integrity: sha512-3Belt6tdc8bPgAtbcmdtNJlirVoTmEb5e2gC94PnkwEW9jI6CAHUeoG85tjWP5WquqfavoMtMwiG4P926ZKKuQ==} + '@kwsites/file-exists@1.1.1': + resolution: {integrity: sha512-m9/5YGR18lIwxSFDwfE3oA7bWuq9kdau6ugN4H2rJeyhFQZcG9AgSHkQtSD15a8WvTgfz9aikZMrKPHvbpqFiw==} + + '@kwsites/promise-deferred@1.1.1': + resolution: {integrity: sha512-GaHYm+c0O9MjZRu0ongGBRbinu8gVAMd2UZjji6jVmqKtZluZnptXGWhz1E8j8D2HJ3f/yMxKAUC0b+57wncIw==} + '@sinclair/typebox@0.27.8': resolution: {integrity: sha512-+Fj43pSMwJs4KRrH/938Uf+uAELIgVBmQzg/q1YG10djyfA3TnrU8N8XzqCh/okZdszqBQTZf96idMfE5lnwTA==} @@ -322,6 +337,22 @@ packages: '@sinonjs/fake-timers@10.3.0': resolution: {integrity: sha512-V4BG07kuYSUkTCSBHG8G8TNhM+F19jXFWnQtzj+we8DrkpSBCee9Z3Ms8yiGer/dlmhe35/Xdgyo3/0rQKg7YA==} + '@trivago/prettier-plugin-sort-imports@5.2.2': + resolution: {integrity: sha512-fYDQA9e6yTNmA13TLVSA+WMQRc5Bn/c0EUBditUHNfMMxN7M82c38b1kEggVE3pLpZ0FwkwJkUEKMiOi52JXFA==} + engines: {node: '>18.12'} + peerDependencies: + '@vue/compiler-sfc': 3.x + prettier: 2.x - 3.x + prettier-plugin-svelte: 3.x + svelte: 4.x || 5.x + peerDependenciesMeta: + '@vue/compiler-sfc': + optional: true + prettier-plugin-svelte: + optional: true + svelte: + optional: true + '@tsconfig/node10@1.0.11': resolution: {integrity: sha512-DcRjDCujK/kCk/cUe8Xz8ZSpm8mS3mNNpta+jGCA6USEDfktlNvm1+IuZ9eTcDbNk41BHwpHHeW+N1lKCz4zOw==} @@ -581,6 +612,10 @@ packages: resolution: {integrity: sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A==} engines: {node: '>=0.3.1'} + docker-compose@1.2.0: + resolution: {integrity: sha512-wIU1eHk3Op7dFgELRdmOYlPYS4gP8HhH1ZmZa13QZF59y0fblzFDFmKPhyc05phCy2hze9OEvNZAsoljrs+72w==} + engines: {node: '>= 6.0.0'} + dunder-proto@1.0.1: resolution: {integrity: sha512-KIN/nDJBQRcXw0MLVhZE9iQHmG68qAVIBg9CqmUYjmQIhgij9U5MFvrqkUL5FbtyyzZuOeOt0zdeRe4UY7ct+A==} engines: {node: '>= 0.4'} @@ -724,6 +759,15 @@ packages: graceful-fs@4.2.11: resolution: {integrity: sha512-RbJ5/jmFcNNCcDV5o9eTnBLJ/HszWV0P73bc+Ff4nS/rJj+YaS6IGyiOL0VoBYX+l1Wrl3k63h/KrH+nhJ0XvQ==} + graphql-request@7.1.2: + resolution: {integrity: sha512-+XE3iuC55C2di5ZUrB4pjgwe+nIQBuXVIK9J98wrVwojzDW3GMdSBZfxUk8l4j9TieIpjpggclxhNEU9ebGF8w==} + peerDependencies: + graphql: 14 - 16 + + graphql@16.11.0: + resolution: {integrity: sha512-mS1lbMsxgQj6hge1XZ6p7GPhbrtFwUFYi3wRzXAC/FmYnyXMTvvI3td3rjmQ2u8ewXueaSvRPWaEcgVVOT9Jnw==} + engines: {node: ^12.22.0 || ^14.16.0 || ^16.0.0 || >=17.0.0} + has-flag@4.0.0: resolution: {integrity: sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==} engines: {node: '>=8'} @@ -818,6 +862,9 @@ packages: engines: {node: '>=10'} hasBin: true + javascript-natural-sort@0.7.1: + resolution: {integrity: sha512-nO6jcEfZWQXDhOiBtG2KvKyEptz7RVbpGP4vTD2hLBdmNQSsCiicO2Ioinv6UI4y9ukqnBpy+XZ9H6uLNgJTlw==} + jest-changed-files@29.7.0: resolution: {integrity: sha512-fEArFiwf1BpQ+4bXSprcDc3/x4HSzL4al2tozwVpDFpsxALjLYdyiIK4e5Vz66GQJIbXJ82+35PtysofptNX2w==} engines: {node: ^14.15.0 || ^16.10.0 || >=18.0.0} @@ -971,6 +1018,9 @@ packages: resolution: {integrity: sha512-eTIzlVOSUR+JxdDFepEYcBMtZ9Qqdef+rnzWdRZuMbOywu5tO2w2N7rqjoANZ5k9vywhL6Br1VRjUIgTQx4E8w==} engines: {node: '>=6'} + lago-javascript-client@1.26.0: + resolution: {integrity: sha512-cUMj/3znu0XXKZPLmvKVKbWobJOqNuKYHhRb7Yqvhw0BPHp5bqKB+b0bcSZKlSiNWF6kSyHeAdhPq9DfuXX8uA==} + leven@3.1.0: resolution: {integrity: sha512-qsda+H8jTaUaN/x5vzW2rzc+8Rw4TAQ/4KjB46IwK5VH+IlVeeeje/EoZRpiXvIqjFgK84QffqPztGI3VBLG1A==} engines: {node: '>=6'} @@ -985,6 +1035,9 @@ packages: lodash.memoize@4.1.2: resolution: {integrity: sha512-t7j+NzmgnQzTAYXcsHYLgimltOV1MXHtlOWf6GjL9Kj8GK5FInw5JotxvbOs+IvV1/Dzo04/fCGfLVs7aXb4Ag==} + lodash@4.17.21: + resolution: {integrity: sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==} + lru-cache@5.1.1: resolution: {integrity: sha512-KpNARQA3Iwv+jTA0utUVVbrh+Jlrr1Fv0e56GGzAFOXN7dk/FviaDW8LHmK52DlcH4WP2n6gI8vN1aesBFgo9w==} @@ -1105,6 +1158,11 @@ packages: resolution: {integrity: sha512-HRDzbaKjC+AOWVXxAU/x54COGeIv9eb+6CkDSQoNTt4XyWoIJvuPsXizxu/Fr23EiekbtZwmh1IcIG/l/a10GQ==} engines: {node: '>=8'} + prettier@3.5.3: + resolution: {integrity: sha512-QQtaxnoDJeAkDvDKWCLiwIXkTgRhwYDEQCghU9Z6q03iyek/rxRh/2lC3HB7P8sWT2xC/y5JDctPLBIGzHKbhw==} + engines: {node: '>=14'} + hasBin: true + pretty-format@29.7.0: resolution: {integrity: sha512-Pdlw/oPxN+aXdmM9R00JVC9WVFoCLTKJvDVLgmJ+qAffBMxsV85l/Lu7sNx4zSzPyoL2euImuEwHhOXdEgNFZQ==} engines: {node: ^14.15.0 || ^16.10.0 || >=18.0.0} @@ -1163,6 +1221,9 @@ packages: signal-exit@3.0.7: resolution: {integrity: sha512-wnD2ZE+l+SPC/uoS0vXeE9L1+0wuaMqKlfz9AMUo38JsyLSBWSFcHR1Rri62LZc12vLr1gb3jl7iwQhgwpAbGQ==} + simple-git@3.27.0: + resolution: {integrity: sha512-ivHoFS9Yi9GY49ogc6/YAi3Fl9ROnF4VyubNylgCkA+RVqLaKWnDSzXOVzya8csELIaWaYNutsEuAhZrtOjozA==} + sisteransi@1.0.5: resolution: {integrity: sha512-bLGGlR1QxBcynn2d5YmDX4MGjlZvy2MRBDRNHLJ8VI6l6+9FUiyTFNJ0IveOSP0bcXgVDPRcfGqA0pjaqUpfVg==} @@ -1547,6 +1608,10 @@ snapshots: dependencies: '@jridgewell/trace-mapping': 0.3.9 + '@graphql-typed-document-node/core@3.2.0(graphql@16.11.0)': + dependencies: + graphql: 16.11.0 + '@istanbuljs/load-nyc-config@1.1.0': dependencies: camelcase: 5.3.1 @@ -1741,6 +1806,14 @@ snapshots: '@jridgewell/resolve-uri': 3.1.2 '@jridgewell/sourcemap-codec': 1.5.0 + '@kwsites/file-exists@1.1.1': + dependencies: + debug: 4.4.0 + transitivePeerDependencies: + - supports-color + + '@kwsites/promise-deferred@1.1.1': {} + '@sinclair/typebox@0.27.8': {} '@sinonjs/commons@3.0.1': @@ -1751,6 +1824,18 @@ snapshots: dependencies: '@sinonjs/commons': 3.0.1 + '@trivago/prettier-plugin-sort-imports@5.2.2(prettier@3.5.3)': + dependencies: + '@babel/generator': 7.27.0 + '@babel/parser': 7.27.0 + '@babel/traverse': 7.27.0 + '@babel/types': 7.27.0 + javascript-natural-sort: 0.7.1 + lodash: 4.17.21 + prettier: 3.5.3 + transitivePeerDependencies: + - supports-color + '@tsconfig/node10@1.0.11': {} '@tsconfig/node12@1.0.11': {} @@ -2026,6 +2111,10 @@ snapshots: diff@4.0.2: {} + docker-compose@1.2.0: + dependencies: + yaml: 2.7.1 + dunder-proto@1.0.1: dependencies: call-bind-apply-helpers: 1.0.2 @@ -2165,6 +2254,13 @@ snapshots: graceful-fs@4.2.11: {} + graphql-request@7.1.2(graphql@16.11.0): + dependencies: + '@graphql-typed-document-node/core': 3.2.0(graphql@16.11.0) + graphql: 16.11.0 + + graphql@16.11.0: {} + has-flag@4.0.0: {} has-symbols@1.1.0: {} @@ -2259,6 +2355,8 @@ snapshots: filelist: 1.0.4 minimatch: 3.1.2 + javascript-natural-sort@0.7.1: {} + jest-changed-files@29.7.0: dependencies: execa: 5.1.1 @@ -2583,6 +2681,8 @@ snapshots: kleur@3.0.3: {} + lago-javascript-client@1.26.0: {} + leven@3.1.0: {} lines-and-columns@1.2.4: {} @@ -2593,6 +2693,8 @@ snapshots: lodash.memoize@4.1.2: {} + lodash@4.17.21: {} + lru-cache@5.1.1: dependencies: yallist: 3.1.1 @@ -2693,6 +2795,8 @@ snapshots: dependencies: find-up: 4.1.0 + prettier@3.5.3: {} + pretty-format@29.7.0: dependencies: '@jest/schemas': 29.6.3 @@ -2738,6 +2842,14 @@ snapshots: signal-exit@3.0.7: {} + simple-git@3.27.0: + dependencies: + '@kwsites/file-exists': 1.1.1 + '@kwsites/promise-deferred': 1.1.1 + debug: 4.4.0 + transitivePeerDependencies: + - supports-color + sisteransi@1.0.5: {} slash@3.0.0: {} diff --git a/t/ts/admin_api.ts b/t/ts/admin_api.ts new file mode 100644 index 000000000000..cccd91da9a85 --- /dev/null +++ b/t/ts/admin_api.ts @@ -0,0 +1,35 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import axios, { type Method, type RawAxiosRequestHeaders } from 'axios'; + +export const request = async ( + uri: string, + method: Method = 'GET', + body?: object, + headers?: RawAxiosRequestHeaders, +) => { + return axios.request({ + method, + // TODO: use 9180 for admin api + url: `http://127.0.0.1:1984/${uri}`, + data: body, + headers: { + 'X-API-KEY': 'edd1c9f034335f136f87ad84b625c8f1', + ...headers, + }, + }); +}; diff --git a/t/ts/utils.ts b/t/ts/utils.ts new file mode 100644 index 000000000000..9112158dce05 --- /dev/null +++ b/t/ts/utils.ts @@ -0,0 +1,18 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +export const wait = (ms: number) => + new Promise((resolve) => setTimeout(resolve, ms)); diff --git a/t/tsconfig.esm.json b/t/tsconfig.esm.json new file mode 100644 index 000000000000..b8b237e3bc05 --- /dev/null +++ b/t/tsconfig.esm.json @@ -0,0 +1,9 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "module": "ESNext", + "moduleResolution": "Bundler", + "isolatedModules": true + }, + "include": ["**/*.mts"] +} diff --git a/t/tsconfig.json b/t/tsconfig.json index 6ede8510b946..d733f24b2e0b 100644 --- a/t/tsconfig.json +++ b/t/tsconfig.json @@ -1,9 +1,11 @@ { "compilerOptions": { - "moduleResolution": "node", "target": "esnext", - "module": "esnext", + "module": "commonjs", "lib": ["esnext"], - "esModuleInterop": true - } + "esModuleInterop": true, + "strict": true, + "noEmit": true + }, + "include": ["**/*.ts"] }