Logo Hor Restfulapi Architecture

Hor Restfulapi Architecture

Tổ chức
openreachtech
hor-restfulapi-architecture

Understand and build the RESTful API layer of a renchan-style backend: the renderer architecture under server/restfulapi/. Use whenever the user asks how the REST API is structured, where an endpoint lives, or how to add or edit one: a Renderer per HTTP method, its route, version and server directory, render(), the response and error hashes, the per-request context and share, the auth filter, flushers, the credential a request carries (cookie, Bearer, IAP JWT, HMAC), and an app behind IAP.

Tổng quan

Nhà phát hànhopenreachtech
Kho mãhora-skills-ort-renchan
Tên kỹ nănghor-restfulapi-architecture
Sao
32
Bản fork
0
Tệp đi kèm
6
Giấy phépApache-2.0
Liên kết
  • Hướng dẫn bằng Markdown

    Tệp SKILL.md được mô hình tải khi cần, nên chỉ tốn token khi có yêu cầu thực sự phù hợp.

  • Dùng được với mọi LLM

    Kỹ năng AI dùng hướng dẫn viết bằng Markdown thay vì mã dành riêng cho một nhà cung cấp, nên dùng được với GPT, Claude, Gemini, Grok hoặc mô hình cục bộ.

  • 6 tệp đi kèm

    Các tập lệnh, mẫu và tài liệu tham khảo mà mô hình có thể đọc khi làm việc. Các tệp chỉ cho phép đọc và không bao giờ được thực thi.

  • Mã nguồn mở

    Được openreachtech công bố trên GitHub. Hãy đọc các tệp nguồn trước khi cài đặt.

Cài đặt

Cài đặt kỹ năng AI Hor Restfulapi Architecture trong TypingMind để dùng với bất kỳ LLM nào, hoặc thêm vào một tác nhân AI khác có thể đọc SKILL.md.

1

Cài đặt trong TypingMind

TypingMind cài đặt kỹ năng trực tiếp từ thư mục GitHub — đọc SKILL.md, đóng gói các tệp tài nguyên và lưu kết quả trên máy.

  1. Mở ứng dụng và vào Plugin → Kỹ năng.
  2. Chọn "Cài đặt từ GitHub".
  3. Dán URL của thư mục kỹ năng được hiển thị bên dưới rồi xác nhận.
    https://github.com/openreachtech/hora-skills-ort-renchan/tree/main/kit/skills/hor-restfulapi-architecture
  4. Bật kỹ năng trong từng cuộc trò chuyện mà bạn muốn sử dụng kỹ năng đó.
Plugin → Kỹ năng → Thêm kỹ năng → Từ URL GitHub, sau đó dán URL thư mục và nhấn Tiếp tục.
2

Cài đặt trong tác nhân AI khác

Bất kỳ tác nhân AI nào đọc được định dạng Agent Skills đều có thể dùng kỹ năng này — hãy sao chép thư mục vào thư mục kỹ năng của tác nhân AI đó.

Claude Code — .claude/skills
git clone --depth 1 https://github.com/openreachtech/hora-skills-ort-renchan.git /tmp/hora-skills-ort-renchan
mkdir -p .claude/skills
cp -r /tmp/hora-skills-ort-renchan/kit/skills/hor-restfulapi-architecture .claude/skills/hor-restfulapi-architecture
Khởi động lại Claude Code sau khi sao chép để ứng dụng nhận kỹ năng mới.

Sử dụng trong TypingMind

Bật Hor Restfulapi Architecture trong bất kỳ cuộc trò chuyện nào trên TypingMind, mô hình sẽ tự xử lý phần còn lại. Tên và mô tả của kỹ năng nằm trong prompt hệ thống. Ngay khi có yêu cầu phù hợp, mô hình tự tải toàn bộ hướng dẫn — bạn không cần gọi thủ công và kỹ năng không tốn token cho đến khi thực sự được dùng.

Mô hình tự nạp Hor Restfulapi Architecture ngay khi có yêu cầu phù hợp.

Dùng được với mọi mô hình AI

Kỹ năng AI là các hướng dẫn Markdown thuần túy, không phải mã dành riêng cho một nhà cung cấp, nên bạn có thể dùng Hor Restfulapi Architecture với cả những mô hình khác ngoài mô hình ban đầu. Chỉ cần cài đặt một lần trong TypingMind rồi dùng với GPT-5, Claude, Gemini, Grok, DeepSeek, Mistral, Llama hoặc mô hình cục bộ do bạn tự chạy — tất cả đều dùng khóa API của riêng bạn.

  • Chỉ tải khi cần

    Prompt hệ thống chỉ chứa tên và mô tả. Hướng dẫn được tải khi có yêu cầu phù hợp đầu tiên, nên khi kỹ năng chưa được dùng, bạn không tốn token cho phần hướng dẫn.

  • Đổi mô hình ngay trong cuộc trò chuyện

    Vì kỹ năng là hướng dẫn chứ không phải mã, việc đổi mô hình không làm kỹ năng ngừng hoạt động — mô hình tiếp theo vẫn đọc cùng tệp SKILL.md.

Hướng dẫn của kỹ năng

Đây là nội dung SKILL.md mà mô hình tải. Hãy đọc trước khi cài đặt — kỹ năng là những hướng dẫn mà mô hình của bạn sẽ làm theo.

RESTful API Architecture

A skill for understanding the REST API layer of a renchan-style backend — the code under server/restfulapi/. Structurally it mirrors the GraphQL layer: there is a per-request context, a per-process share, an engine that wires everything to the server, and an error hash declared up front. The difference is the surface: instead of a resolver returning a plain object, a renderer returns a RestfulApiResponse carrying an HTTP status code plus content or error, and a flusher writes it to the response.

This skill is self-contained. Support classes (BaseRestfulApiServerEngine, BaseGetRenderer / BasePostRenderer, RestfulApiResponse, BaseRestfulApiContext / BaseRestfulApiShare, BaseRestfulApiResponseFlusher) come from the framework (@openreachtech/renchan). Examples use generic entities (Article) and generic routes; substitute your own. Some conventions below are the target shape — where the current repo diverges (notably input validation, §5), the improved form is described.

Core principle: a renderer is a resolver with an HTTP envelope

One request → one renderer. A renderer does the same job a GraphQL resolver does — validate, touch the DB / an external system, shape a result — but it speaks HTTP:

  • Its return value is a RestfulApiResponse (statusCode + content / error), not a bare object. Errors are returned, not thrown: return this.errorResponseHash.Xxx.createAsError().
  • It is bound to an HTTP method + route (GET /v1/articles), discovered from a versioned directory (renderers/v1/get/…), not from a schema name.
  • Everything else — the per-request context (now, share, env, the authenticated user), the error hash declared up front, an optional DI factory — is the same idea as the GraphQL side.

Three rules matter most:

  1. Return responses, never throw. Every exit is a RestfulApiResponse — success via RestfulApiResponse.create({ statusCode, content }), failure via this.errorResponseHash.Xxx.createAsError(). HTTP always needs a status code, so an error without one is meaningless here (§4).
  2. The engine owns cross-cutting concerns; the renderer owns one endpoint. Auth (the visa filter), middleware, versioning, and the standard error envelopes live on the engine (§3). The renderer only knows its route, its errors, and its render() (§2).
  3. Validate at the boundary, then delegate. REST input arrives split across body / query / path params and is all string-like; normalize and validate it before touching the DB (§5).
  • Comments: English for code, the surrounding language for domain notes. Match the neighbors.

Layer map

                        HTTP request
                             │
        ┌────────────────────▼─────────────────────┐
        │ Engine  (BaseRestfulApiServerEngine)      │  app-wide: routes, middleware,
        │  config: pathPrefix + renderersPath       │  auth filter (visa), Share/Context,
        │  generateFilterHandler(), visaIssuers     │  standardErrorEnvelopHash
        └────────────────────┬─────────────────────┘
                             │ builds per request
        ┌────────────────────▼─────────────────────┐
        │ Context (per request) + Share (per proc)  │  now, env, share, user,
        │  hasAuthenticated / hasAuthorized / …     │  hasPathPermission  (§3)
        └────────────────────┬─────────────────────┘
                             │ render input = { body, query, context, request }
        ┌────────────────────▼─────────────────────┐
        │ Renderer (BaseGetRenderer/BasePostRenderer)│  routePath, errorStructureHash,
        │  passesFilter, render() → RestfulApiResponse│  passesFilter, render()  (§1,§2)
        └────────────────────┬─────────────────────┘
                             │ returns RestfulApiResponse (status + content/error)  (§4)
        ┌────────────────────▼─────────────────────┐
        │ Flusher (BaseRestfulApiResponseFlusher)   │  writes status + headers + body
        │  Json (default) / Html / Csv / Redirect   │  contentType, flushResponseBody  (§4)
        └────────────────────┬─────────────────────┘
                             ▼
                        HTTP response

Request lifecycle

For each incoming request the framework's routes builder:

  1. Builds the render input { body, query, context, request } — body is the multer-fulfilled request body, query is expressRequest.query, context is created async (resolves the user + visa), request wraps the express request (path-param proxy).
  2. If the renderer's passesFilter is true, skips the filter; otherwise runs the engine's filter handler (auth). If the filter returns an error response, that short-circuits render().
  3. Calls renderer.render(input), which returns a RestfulApiResponse.
  4. Any thrown error is caught and converted to a 500 RestfulApiResponse (Unknown) — so renderers should still return their own typed errors rather than relying on this.
  5. The renderer's flusher writes the response (status, headers, body).

1. Directory, versioning, and route binding

Renderers live under server/restfulapi/renderers/, split by version then by HTTP method:

server/restfulapi/
  AppRestfulApiServerEngine.js       # the engine (§3)
  contexts/
    AppRestfulApiContext.js          # per-request context (§3)
    AppRestfulApiShare.js            # per-process share (§3)
  flushers/                          # custom response flushers (§4)
  renderers/
    v1/
      get/   <Name>Renderer.js       # one file per endpoint
      post/  <Name>Renderer.js
    v2/
      get/   …
      post/  …
  • renderers/<version>/<method>/<Name>Renderer.js — the directory encodes the version (v1, v2) and the HTTP method (get, post, …). Keep the method out of the class name and let the folder carry it (the repo has both FooRenderer and FooGetRenderer — prefer the folder-only form for new files).

  • The engine selects the version. config.renderersPath points at one version directory (renderers/v1/) and config.pathPrefix (/v1) is prepended to every routePath. So routePath = '/articles' in renderers/v1/get/ serves GET /v1/articles. Serve another version by pointing an engine at renderers/v2/ with prefix /v2.

  • The route is pathPrefix + routePath, method = the renderer's method. The framework discovers every renderer under renderersPath, reads its routePath and method, and registers the express route — you never wire routes by hand.

  • Several RESTful API servers: one directory per server. When a project runs more than one server (for example one public and one reachable only inside a VPN), each server's routes live under renderers/<server>/v1/, and each server's engine points renderersPath at its own renderers/<server>/v1/. A single renderers/v1/ shared by two engines would mount every route on both servers, so the VPN-only routes would answer on the public one. A project with one server keeps renderers/v1/.

server/restfulapi/renderers/
  public/
    v1/
      get/   <Name>Renderer.js
  vpn/
    v1/
      get/   <Name>Renderer.js

Detail in renderers.md.

2. The renderer

A renderer extends the method base (BaseGetRenderer / BasePostRenderer / BasePutRenderer / …) and implements a small, fixed surface:

js
import {
  BaseGetRenderer,
  RestfulApiResponse,
} from '@openreachtech/renchan'

export default class ArticleGetRenderer extends BaseGetRenderer {
  /** @override */
  static get routePath () {
    return '/articles'
  }

  /** @override */
  static get errorStructureHash () {
    return {
      ArticleNotFound: {
        statusCode: 404,
        errorMessage: 'Article not found',
      },
    }
  }

  /** @override */
  get passesFilter () {
    return true // public endpoint → skip the engine auth filter
  }

  /**
   * @override
   * @param {RestfulApiType.RenderInput<*, *>} input
   * @returns {Promise<RestfulApiType.RenderResponse>}
   */
  async render ({
    query,
    context,
    request,
  }) {
    const article = await this.findArticle({
      articleId: request.pathParameterHashProxy.articleId,
    })

    if (!article) {
      return this.errorResponseHash.ArticleNotFound.createAsError()
    }

    return RestfulApiResponse.create({
      statusCode: 200,
      content: this.formatResponseContent({
        article,
      }),
    })
  }

  // ... findArticle(), formatResponseContent() ...
}
  • render({ body, query, context, request }) is the entry point. body = parsed request body (POST/PUT/PATCH), query = query string params, context = the per-request context (now, share, env, user — §3), request = the wrapped express request (expressRequest, and a pathParameterHashProxy that returns null for missing path params).
  • static get routePath() — the route (prefixed by the engine, §1).
  • get passesFilter() — true skips the engine's auth filter (public); default false runs it.
  • Dependency injection — add constructor + static create() + static createXxx() only when a tool must be substitutable in tests (same factory pattern as the GraphQL side).

Method-verb subclasses, render() input shape, DI, and file uploads (multer via fileFieldsConfigHash) are in renderers.md.

3. Engine, context, and share (the app-wide layer)

These three set up the whole REST surface, mirroring the GraphQL engine/context/share:

  • Engine (AppRestfulApiServerEngine extends BaseRestfulApiServerEngine) — declares config (pathPrefix / renderersPath / staticPath), wires static get Share() / static get Context(), the standardErrorEnvelopHash (cross-cutting errors: Unknown, Unauthenticated, Unauthorized, Database, …), the express middleware (collectMiddleware()), the auth filter (generateFilterHandler()), and the visa issuers (visaIssuers: hasAuthenticated / hasAuthorized / hasPathPermission).
  • Context (AppRestfulApiContext extends BaseRestfulApiContext) — built per request. Override static async findUser() to resolve the authenticated user from the access-token header, or static extractAccessToken() as well to read the credential from another route (a cookie, a Bearer token, an IAP JWT, an HMAC signature). Exposes now, userId, userEntity, share, env, and the visa predicates (hasAuthenticated() / hasAuthorized() / hasPathPermission()).
  • Share (AppRestfulApiShare extends BaseRestfulApiShare) — the per-process object (holds env; put shared clients/providers here). Often a // noop subclass.

Detail in engine-and-context.md.

4. Response, error hash, and flusher

  • RestfulApiResponse is the return type. Success: RestfulApiResponse.create({ statusCode, headers?, content }). Error: from the built error hash, this.errorResponseHash.Xxx.createAsError().
  • Error hash — like the GraphQL error code hash, a renderer declares its errors up front in static get errorStructureHash(). Unlike GraphQL (code only), each REST entry also carries a statusCode and errorMessage, because an HTTP response needs a status. The framework turns the hash into constructable RestfulApiResponse subclasses on this.errorResponseHash. Cross-cutting errors live on the engine's standardErrorEnvelopHash.
  • Extending the response — when you need a non-default body shape or behavior, subclass RestfulApiResponse in the app and return that.
  • Flusher — a BaseRestfulApiResponseFlusher writes the response (status → headers → body). Default is JSON ({ content, error }); the framework also ships Html and Csv; a renderer selects one via static get FlusherCtor(), and the app can define custom flushers (e.g. a redirect).

Detail in response-and-flusher.md.

5. Input validation (the target shape)

The current repo validates REST input ad hoc inside render() (an inline isValidRequestBody()). The target is to validate with the same validator structure as GraphQL: a BaseInputValidator subclass whose generateValidationEntries() returns [predicate, ErrorCtor] pairs and whose validateInput() returns the first failing error (or null).

The catch is the input shape. A GraphQL validator receives a single typed input object; REST input arrives split across body / query / path params, all strings. So a REST renderer needs an adapter: a small class that reads { body, query, request } and produces the normalized input object the validator expects. The renderer flow becomes adapter → input → validator → (on failure) map to the renderer's errorResponseHash entry.

There is also a type-mismatch to bridge: a GraphQL-style validator yields a code-only error, while REST needs a status-bearing RestfulApiResponse. The wiring must map each validation failure to the matching errorStructureHash entry (status + message).

The adapter, the validator contract, and the error mapping are in validation.md.

Detail files

  • renderers.md — the renderer file: method-verb subclasses, directory & versioning, routePath, render() input (body / query / context / request), passesFilter, the DI factory, and multipart file uploads (§1, §2)
  • engine-and-context.md — the engine (config, renderersPath / pathPrefix, Share / Context, standardErrorEnvelopHash, middleware, filter handler, visa issuers) and the per-request context / per-process share (§3)
  • credential-routes.md covers reading the credential from a cookie, a Bearer token, an IAP JWT or an HMAC signature by overriding extractAccessToken() on the context, with the CSRF and replay rules each route brings (§3)
  • behind-iap.md covers an application behind IAP with no sign-in of its own: the IAP JWT and its claims, the verifier, a context that finds the actor by sub, and what the filter skips (§3)
  • response-and-flusher.md — RestfulApiResponse (success / createAsError), errorStructureHash → errorResponseHash, extending the response, and flushers (JSON / Html / Csv / custom, FlusherCtor, contentType) (§4)
  • validation.md — the target validator (BaseInputValidator, generateValidationEntries, validateInput), the REST-input → input adapter, and mapping a validation failure to a status-bearing response (§5)

Tệp đi kèm

Khi kỹ năng đã được tải, mô hình có thể đọc các tệp này nếu cần. Các tệp này chỉ dùng để đọc và không bao giờ được thực thi.

Câu hỏi thường gặp

Kỹ năng AI Hor Restfulapi Architecture làm được gì?

Understand and build the RESTful API layer of a renchan-style backend: the renderer architecture under server/restfulapi/. Use whenever the user asks how the REST API is structured, where an endpoint lives, or how to add or edit one: a Renderer per HTTP method, its route, version and server directory, render(), the response and error hashes, the per-request context and share, the auth filter, flushers, the credential a request carries (cookie, Bearer, IAP JWT, HMAC), and an app behind IAP.

Vì sao nên dùng Hor Restfulapi Architecture trên TypingMind?

Vì bạn chỉ cần cài một lần rồi dùng với bất kỳ mô hình nào. Hor Restfulapi Architecture là nội dung Markdown chứ không phải mã dành riêng cho từng nhà cung cấp, nên cùng một kỹ năng có thể chạy trên GPT-5, Claude, Gemini, Grok hoặc mô hình trên thiết bị — và bạn có thể đổi mô hình ngay trong cuộc trò chuyện mà kỹ năng vẫn hoạt động. TypingMind dùng khóa API riêng của bạn, nên bạn thanh toán trực tiếp cho nhà cung cấp thay vì trả phí thuê bao theo suất người dùng, đồng thời kỹ năng và các cuộc trò chuyện vẫn nằm trong bộ lưu trữ của bạn.

Làm thế nào để cài Hor Restfulapi Architecture trong TypingMind?

Mở Plugin → Kỹ năng → Cài đặt từ GitHub trong TypingMind rồi dán https://github.com/openreachtech/hora-skills-ort-renchan/tree/main/kit/skills/hor-restfulapi-architecture. TypingMind đọc tệp SKILL.md, đóng gói các tệp đi kèm rồi cài đặt cả gói dưới dạng một kỹ năng mà bạn có thể bật cho từng cuộc trò chuyện.

Những mô hình AI nào có thể dùng Hor Restfulapi Architecture?

Bất kỳ mô hình nào bạn kết nối trong TypingMind. Kỹ năng AI là các hướng dẫn bằng Markdown chứ không phải mã dành riêng cho từng nhà cung cấp, nên GPT, Claude, Gemini, Grok và các mô hình chạy trên thiết bị đều có thể tải kỹ năng này khi có yêu cầu phù hợp.

Có thể dùng Hor Restfulapi Architecture với bao nhiêu mô hình AI?

Bao nhiêu cũng được. Miễn là mô hình hỗ trợ kỹ năng, bạn có thể dùng Hor Restfulapi Architecture với mô hình đó — GPT, Claude, Gemini, Grok, DeepSeek, Mistral, Llama và nhiều mô hình khác — tất cả đều trên TypingMind với khóa API riêng của bạn.

Kỹ năng AI Hor Restfulapi Architecture có miễn phí không?

Có. Kỹ năng này được openreachtech phát hành trên GitHub theo giấy phép Apache-2.0. Bạn chỉ trả tiền cho nhà cung cấp AI của mình theo số token đã dùng.

Kỹ năng AI là gì?

Kỹ năng AI là một gói hướng dẫn có thể tái sử dụng, chỉ cho mô hình AI cách thực hiện một tác vụ cụ thể. Kỹ năng tuân theo định dạng mở Agent Skills: một tệp SKILL.md chứa tên và mô tả, cùng các tập lệnh, mẫu hoặc tệp tham khảo mà mô hình có thể cần. Mô hình chỉ đọc hướng dẫn khi yêu cầu của bạn phù hợp với kỹ năng, nên kỹ năng đã cài đặt không phát sinh chi phí cho đến khi được sử dụng.

Kỹ năng AI khác gì với plugin hoặc máy chủ MCP?

Plugin hoặc máy chủ MCP cung cấp các công cụ mới mà mô hình có thể gọi. Khi mô hình gọi một công cụ, mã của công cụ đó chạy ở đâu đó và trả về kết quả. Còn kỹ năng AI cung cấp kiến thức và quy trình: cách tiếp cận tác vụ, các bước cần thực hiện và những tiêu chí mà kết quả cần đáp ứng. Kỹ năng chỉ là nội dung Markdown, nên không cần máy chủ, khóa API hay môi trường thực thi và hoạt động với mọi mô hình.

Xem tất cả

Tạo ngay không gian làm việc AI riêng

Đăng ký bản tin của chúng tôi để nhận thông báo về tính năng mới và các đợt tặng quà sắp tới 👇