From 718a98e80b28d9507fcf61d9e7687da923964bcb Mon Sep 17 00:00:00 2001 From: Ophir Lojkine Date: Sat, 28 Nov 2026 23:46:31 +0000 Subject: [PATCH] Load function references from Markdown, starting with request context --- examples/official-site/README.md | 7 ++ .../functions/client_ip.md} | 29 +++--- .../official-site/content/functions/cookie.md | 39 ++++++++ .../official-site/content/functions/header.md | 23 +++++ .../functions/headers.md} | 26 +++--- .../21_path.sql => content/functions/path.md} | 22 ++--- .../content/functions/protocol.md | 26 ++++++ .../functions/request_method.md} | 31 +++---- .../sqlpage/migrations/08_functions.sql | 91 ------------------- .../sqlpage/migrations/27_protocol.sql | 30 ------ .../migrations/82_reference_catalog.sql | 7 -- scripts/site/content.mjs | 17 ++++ scripts/site/documentation.mjs | 2 + scripts/site/functions.mjs | 38 ++++++++ 14 files changed, 195 insertions(+), 193 deletions(-) rename examples/official-site/{sqlpage/migrations/57_client_ip.sql => content/functions/client_ip.md} (63%) create mode 100644 examples/official-site/content/functions/cookie.md create mode 100644 examples/official-site/content/functions/header.md rename examples/official-site/{sqlpage/migrations/56_headers.sql => content/functions/headers.md} (56%) rename examples/official-site/{sqlpage/migrations/21_path.sql => content/functions/path.md} (53%) create mode 100644 examples/official-site/content/functions/protocol.md rename examples/official-site/{sqlpage/migrations/43_request_method.sql => content/functions/request_method.md} (58%) delete mode 100644 examples/official-site/sqlpage/migrations/27_protocol.sql create mode 100644 scripts/site/functions.mjs diff --git a/examples/official-site/README.md b/examples/official-site/README.md index 1aabe20f6..478528ae0 100644 --- a/examples/official-site/README.md +++ b/examples/official-site/README.md @@ -38,6 +38,13 @@ Edit `content/guides.yaml` for the guides list and `content/projects.yaml` for t example project cards. A project's `folder` must match its folder under [`examples/`](../). +## Function reference + +Function pages in `content/functions/` use their filename as the public function +name. Keep the argument list in signature order and mark mandatory arguments with +`required: true`. The Markdown body can include SQL examples without escaping. +Older entries still live in the SQL migrations. + ## Landing page The home page is rendered by `index.sql` and the `landing-page` custom template. diff --git a/examples/official-site/sqlpage/migrations/57_client_ip.sql b/examples/official-site/content/functions/client_ip.md similarity index 63% rename from examples/official-site/sqlpage/migrations/57_client_ip.sql rename to examples/official-site/content/functions/client_ip.md index 168451f40..ef5afe844 100644 --- a/examples/official-site/sqlpage/migrations/57_client_ip.sql +++ b/examples/official-site/content/functions/client_ip.md @@ -1,16 +1,11 @@ -INSERT INTO - sqlpage_functions ( - "name", - "introduced_in_version", - "icon", - "description_md" - ) -VALUES - ( - 'client_ip', - '0.33.0', - 'network', - 'Returns the IP address of the client making the HTTP request. +--- +icon: network +since: 0.33.0 +category: request-data +summary: Get the IP address of the client making the request. +--- + +Returns the IP address of the client making the HTTP request. ### Example @@ -28,19 +23,17 @@ The function returns: When [running behind a reverse proxy](/your-first-sql-website/nginx.sql) (e.g., Nginx, Apache, Cloudflare): - This function will return the IP address of the reverse proxy, not the actual client -- To get the real client IP, use [`sqlpage.header`](?function=header): `sqlpage.header(''x-forwarded-for'')` or `sqlpage.header(''x-real-ip'')` +- To get the real client IP, use [`sqlpage.header`](?function=header): `sqlpage.header('x-forwarded-for')` or `sqlpage.header('x-real-ip')` - The exact header name depends on your reverse proxy configuration Example with reverse proxy: ```sql -- Choose the appropriate header based on your setup select coalesce( - sqlpage.header(''x-forwarded-for''), - sqlpage.header(''x-real-ip''), + sqlpage.header('x-forwarded-for'), + sqlpage.header('x-real-ip'), sqlpage.client_ip() ) as real_client_ip; ``` For security-critical applications, ensure your reverse proxy is properly configured to set and validate these headers. -' - ); \ No newline at end of file diff --git a/examples/official-site/content/functions/cookie.md b/examples/official-site/content/functions/cookie.md new file mode 100644 index 000000000..5f92673c1 --- /dev/null +++ b/examples/official-site/content/functions/cookie.md @@ -0,0 +1,39 @@ +--- +icon: cookie +since: 0.7.1 +category: request-data +summary: Read a named cookie from the incoming request. +return_type: TEXT +parameters: + - name: name + type: TEXT + required: true + description: The name of the cookie to read. +--- + +Reads a [cookie](https://en.wikipedia.org/wiki/HTTP_cookie) with the given name from the request. +Returns the value of the cookie as text, or NULL if the cookie is not present. + +Cookies can be set using the [cookie component](documentation.sql?component=cookie#component). + +### Example + +#### Set a cookie + +Set a cookie called `username` to greet the user by name every time they visit the page: + +```sql +select 'cookie' as component, 'username' as name, :username as value; + +SELECT 'form' as component; +SELECT 'username' as name, 'text' as type; +``` + +#### Read a cookie + +Read a cookie called `username` and greet the user by name: + +```sql +SELECT 'text' as component, + 'Hello, ' || sqlpage.cookie('username') || '!' as contents; +``` diff --git a/examples/official-site/content/functions/header.md b/examples/official-site/content/functions/header.md new file mode 100644 index 000000000..7ee40e529 --- /dev/null +++ b/examples/official-site/content/functions/header.md @@ -0,0 +1,23 @@ +--- +icon: heading +since: 0.7.2 +category: request-data +summary: Read the value of one HTTP request header. +parameters: + - name: name + type: TEXT + required: true + description: The name of the HTTP header to read. +--- + +Reads one [HTTP request header](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers). Names are case-insensitive: `sqlpage.header('User-Agent')` and `sqlpage.header('user-agent')` read the same value. An absent header returns SQL `NULL`. + +### Example + +Log the [`User-Agent`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/User-Agent) of the browser making the request in the database: + +```sql +INSERT INTO user_agent_log (user_agent) VALUES (sqlpage.header('user-agent')); +``` + +If you need access to all headers at once, use [`sqlpage.headers()`](?function=headers) instead. diff --git a/examples/official-site/sqlpage/migrations/56_headers.sql b/examples/official-site/content/functions/headers.md similarity index 56% rename from examples/official-site/sqlpage/migrations/56_headers.sql rename to examples/official-site/content/functions/headers.md index b88aaf4e2..dc75859b4 100644 --- a/examples/official-site/sqlpage/migrations/56_headers.sql +++ b/examples/official-site/content/functions/headers.md @@ -1,28 +1,25 @@ -INSERT INTO sqlpage_functions ( - "name", - "introduced_in_version", - "icon", - "description_md" - ) -VALUES ( - 'headers', - '0.33.0', - 'circle-dotted-letter-h', - 'Returns all HTTP request headers as a JSON object. +--- +icon: circle-dotted-letter-h +since: 0.33.0 +category: request-data +summary: Read all HTTP request headers as a JSON object. +--- + +Returns all HTTP request headers as a JSON object. ### Example The following displays all HTTP request headers in a list, -using SQLite''s `json_each()` function. +using SQLite's `json_each()` function. ```sql -select ''list'' as component; +select 'list' as component; select key as title, value as description from json_each(sqlpage.headers()); -- json_each() is SQLite only ``` -If not on SQLite, use your [database''s JSON function](/blog.sql?post=JSON%20in%20SQL%3A%20A%20Comprehensive%20Guide). +If not on SQLite, use your [database's JSON function](/blog.sql?post=JSON%20in%20SQL%3A%20A%20Comprehensive%20Guide). ### Details @@ -36,4 +33,3 @@ This is useful when you need to: - Access multiple headers at once If you only need access to a single known header, use [`sqlpage.header(name)`](?function=header) instead. -'); \ No newline at end of file diff --git a/examples/official-site/sqlpage/migrations/21_path.sql b/examples/official-site/content/functions/path.md similarity index 53% rename from examples/official-site/sqlpage/migrations/21_path.sql rename to examples/official-site/content/functions/path.md index 5fc1afaa4..2e3101e37 100644 --- a/examples/official-site/sqlpage/migrations/21_path.sql +++ b/examples/official-site/content/functions/path.md @@ -1,14 +1,11 @@ -INSERT INTO sqlpage_functions ( - "name", - "introduced_in_version", - "icon", - "description_md" - ) -VALUES ( - 'path', - '0.15.0', - 'slashes', - 'Returns the request path of the current page. +--- +icon: slashes +since: 0.15.0 +category: request-data +summary: Get the URL path of the current request. +--- + +Returns the request path of the current page. This is useful to generate links to the current page, and when you have a proxy in front of your SQLPage server that rewrites the URL. ### Example @@ -17,10 +14,9 @@ If we have a page in a file named `my page.sql` at the root of your SQLPage inst then the following SQL query: ```sql -select ''text'' as component, sqlpage.path() as contents; +select 'text' as component, sqlpage.path() as contents; ``` will return `/my%20page.sql`. > Note that the path is URL-encoded. -'); \ No newline at end of file diff --git a/examples/official-site/content/functions/protocol.md b/examples/official-site/content/functions/protocol.md new file mode 100644 index 000000000..3578c54ff --- /dev/null +++ b/examples/official-site/content/functions/protocol.md @@ -0,0 +1,26 @@ +--- +icon: network +since: 0.17.1 +category: request-data +summary: Identify whether the request used HTTP or HTTPS. +--- + +Returns the protocol that was used to access the current page. + +This can be either `http` or `https`. + +This is useful to generate links to the current page. + +### Example + +```sql +select 'text' as component, + sqlpage.protocol() || '://' || sqlpage.header('host') || sqlpage.path() as contents; +``` + +will return `https://example.com/example.sql`. + +> Note that the path is URL-encoded. The protocol is resolved in this order: +> - `Forwarded` header +> - `X-Forwarded-Proto` header +> request target / URI diff --git a/examples/official-site/sqlpage/migrations/43_request_method.sql b/examples/official-site/content/functions/request_method.md similarity index 58% rename from examples/official-site/sqlpage/migrations/43_request_method.sql rename to examples/official-site/content/functions/request_method.md index b4beb545d..01577e059 100644 --- a/examples/official-site/sqlpage/migrations/43_request_method.sql +++ b/examples/official-site/content/functions/request_method.md @@ -1,22 +1,17 @@ -INSERT INTO - sqlpage_functions ( - "name", - "introduced_in_version", - "icon", - "description_md" - ) -VALUES - ( - 'request_method', - '0.21.0', - 'http-get', - 'Returns the HTTP request method (GET, POST, etc.) used to access the page. +--- +icon: http-get +since: 0.21.0 +category: request-data +summary: Get the HTTP method, such as GET or POST. +--- + +Returns the HTTP request method (GET, POST, etc.) used to access the page. ## HTTP request methods HTTP request methods (also known as verbs) are used to indicate the desired action to be performed on the identified resource. The most common methods are: - **GET**: retrieve information from the server. This is the default method used by browsers when you click on a link. - - **POST**: submit data to be processed by the server. SQLPage''s [form component](/component.sql?component=form) defaults to POST; a plain HTML form defaults to GET. + - **POST**: submit data to be processed by the server. SQLPage's [form component](/component.sql?component=form) defaults to POST; a plain HTML form defaults to GET. - **PUT**: replace the current representation of the target resource with the request payload. Most commonly used in REST APIs. - **DELETE**: remove the target resource. - **PATCH**, **HEAD**, **OPTIONS**, **CONNECT**, **TRACE**: less common methods that are used in specific situations. @@ -24,11 +19,9 @@ HTTP request methods (also known as verbs) are used to indicate the desired acti ## Require a PUT request ```sql -select ''redirect'' as component, - ''/error?msg=expected+a+PUT+request'' as link -where sqlpage.request_method() != ''PUT''; +select 'redirect' as component, + '/error?msg=expected+a+PUT+request' as link +where sqlpage.request_method() != 'PUT'; insert into my_table (column1, column2) values (:value1, :value2); ``` -' - ); \ No newline at end of file diff --git a/examples/official-site/sqlpage/migrations/08_functions.sql b/examples/official-site/sqlpage/migrations/08_functions.sql index 335729e56..3620a668b 100644 --- a/examples/official-site/sqlpage/migrations/08_functions.sql +++ b/examples/official-site/sqlpage/migrations/08_functions.sql @@ -12,97 +12,6 @@ CREATE TABLE IF NOT EXISTS sqlpage_function_parameters ( "description_md" TEXT, "type" TEXT ); -INSERT INTO sqlpage_functions ( - "name", - "return_type", - "introduced_in_version", - "icon", - "description_md" - ) -VALUES ( - 'cookie', - 'TEXT', - '0.7.1', - 'cookie', - 'Reads a [cookie](https://en.wikipedia.org/wiki/HTTP_cookie) with the given name from the request. -Returns the value of the cookie as text, or NULL if the cookie is not present. - -Cookies can be set using the [cookie component](documentation.sql?component=cookie#component). - -### Example - -#### Set a cookie - -Set a cookie called `username` to greet the user by name every time they visit the page: - -```sql -select ''cookie'' as component, ''username'' as name, :username as value; - -SELECT ''form'' as component; -SELECT ''username'' as name, ''text'' as type; -``` - -#### Read a cookie - -Read a cookie called `username` and greet the user by name: - -```sql -SELECT ''text'' as component, - ''Hello, '' || sqlpage.cookie(''username'') || ''!'' as contents; -``` -' - ); -INSERT INTO sqlpage_function_parameters ( - "function", - "index", - "name", - "description_md", - "type" - ) -VALUES ( - 'cookie', - 1, - 'name', - 'The name of the cookie to read.', - 'TEXT' - ); -INSERT INTO sqlpage_functions ( - "name", - "introduced_in_version", - "icon", - "description_md" - ) -VALUES ( - 'header', - '0.7.2', - 'heading', - 'Reads one [HTTP request header](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers). Names are case-insensitive: `sqlpage.header(''User-Agent'')` and `sqlpage.header(''user-agent'')` read the same value. An absent header returns SQL `NULL`. - -### Example - -Log the [`User-Agent`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/User-Agent) of the browser making the request in the database: - -```sql -INSERT INTO user_agent_log (user_agent) VALUES (sqlpage.header(''user-agent'')); -``` - -If you need access to all headers at once, use [`sqlpage.headers()`](?function=headers) instead. - ' - ); -INSERT INTO sqlpage_function_parameters ( - "function", - "index", - "name", - "description_md", - "type" - ) -VALUES ( - 'header', - 1, - 'name', - 'The name of the HTTP header to read.', - 'TEXT' - ); INSERT INTO sqlpage_functions ( "name", "return_type", diff --git a/examples/official-site/sqlpage/migrations/27_protocol.sql b/examples/official-site/sqlpage/migrations/27_protocol.sql deleted file mode 100644 index 1b0e155c1..000000000 --- a/examples/official-site/sqlpage/migrations/27_protocol.sql +++ /dev/null @@ -1,30 +0,0 @@ -INSERT INTO sqlpage_functions ( - "name", - "introduced_in_version", - "icon", - "description_md" - ) -VALUES ( - 'protocol', - '0.17.1', - 'network', - 'Returns the protocol that was used to access the current page. - -This can be either `http` or `https`. - -This is useful to generate links to the current page. - -### Example - -```sql -select ''text'' as component, - sqlpage.protocol() || ''://'' || sqlpage.header(''host'') || sqlpage.path() as contents; -``` - -will return `https://example.com/example.sql`. - -> Note that the path is URL-encoded. The protocol is resolved in this order: -> - `Forwarded` header -> - `X-Forwarded-Proto` header -> request target / URI -'); \ No newline at end of file diff --git a/examples/official-site/sqlpage/migrations/82_reference_catalog.sql b/examples/official-site/sqlpage/migrations/82_reference_catalog.sql index 3f5be829d..4cd49c460 100644 --- a/examples/official-site/sqlpage/migrations/82_reference_catalog.sql +++ b/examples/official-site/sqlpage/migrations/82_reference_catalog.sql @@ -92,15 +92,8 @@ UPDATE component SET (catalog_category, catalog_summary) = (copy.category, copy. FROM copy WHERE component.name = copy.name; WITH copy(name, category, summary) AS (VALUES - ('client_ip', 'request-data', 'Get the IP address of the client making the request.'), - ('cookie', 'request-data', 'Read a named cookie from the incoming request.'), - ('header', 'request-data', 'Read the value of one HTTP request header.'), - ('headers', 'request-data', 'Read all HTTP request headers as a JSON object.'), - ('path', 'request-data', 'Get the URL path of the current request.'), - ('protocol', 'request-data', 'Identify whether the request used HTTP or HTTPS.'), ('request_body', 'request-data', 'Read the raw request body as text.'), ('request_body_base64', 'request-data', 'Read a binary request body as a Base64 string.'), - ('request_method', 'request-data', 'Get the HTTP method, such as GET or POST.'), ('variables', 'request-data', 'Read request or page variables as a JSON object.'), ('link', 'links-and-routing', 'Build a page URL with optional query parameters and a fragment.'), diff --git a/scripts/site/content.mjs b/scripts/site/content.mjs index 369860330..49104ed6d 100644 --- a/scripts/site/content.mjs +++ b/scripts/site/content.mjs @@ -52,3 +52,20 @@ export function text(value, name) { throw new Error(`Expected text in ${name}`); return value; } + +export function parameter(meta) { + const { + name, + type, + description, + required = false, + } = fields(meta, ["name", "type", "description", "required"]); + if (typeof required !== "boolean") + throw new Error(`Expected a boolean for ${name}.required`); + return { + name: text(name, "name"), + type: text(type, "type"), + description: text(description, "description"), + optional: required ? 0 : 1, + }; +} diff --git a/scripts/site/documentation.mjs b/scripts/site/documentation.mjs index d8a92c34d..488f71504 100644 --- a/scripts/site/documentation.mjs +++ b/scripts/site/documentation.mjs @@ -3,6 +3,7 @@ import { join } from "node:path"; import { DatabaseSync } from "node:sqlite"; import { fields, pages, text } from "./content.mjs"; import { insertDirectories } from "./directories.mjs"; +import { insertFunctions } from "./functions.mjs"; // Let SQLite quote bound values for the migration exactly as it executed them. export class Seed { @@ -70,6 +71,7 @@ export function writeDocumentation(content, migrations) { const seed = new Seed(db); insertDirectories(seed, content); insertPosts(seed, join(content, "blog")); + insertFunctions(seed, join(content, "functions")); writeFileSync( join(migrations, `${version}_documentation_content.sql`), seed.toString(), diff --git a/scripts/site/functions.mjs b/scripts/site/functions.mjs new file mode 100644 index 000000000..6ed8e3690 --- /dev/null +++ b/scripts/site/functions.mjs @@ -0,0 +1,38 @@ +import { fields, pages, parameter } from "./content.mjs"; + +export function insertFunctions(seed, dir) { + pages(dir, (name, meta, body) => { + const f = fields(meta, [ + "icon", + "since", + "category", + "summary", + "return_type", + "parameters", + ]); + seed.row( + `INSERT INTO sqlpage_functions (name, icon, description_md, return_type, introduced_in_version, catalog_category, catalog_summary) + VALUES (?, ?, ?, ?, ?, ?, ?)`, + name, + f.icon ?? null, + body, + f.return_type ?? null, + f.since ?? null, + f.category ?? null, + f.summary ?? null, + ); + for (const [index, entry] of (f.parameters ?? []).entries()) { + const p = parameter(entry); + seed.row( + `INSERT INTO sqlpage_function_parameters ("function", "index", name, description_md, type, optional) + VALUES (?, ?, ?, ?, ?, ?)`, + name, + index + 1, + p.name, + p.description, + p.type, + p.optional, + ); + } + }); +}