PageSourceSearch

https://www.oprajs.com/assets/js/e9ec4696.84babce6.js

js oprajs.com collected 2026-10-05 06:33:03 UTC 12,492 bytes, 1 lines download raw bytes

1"use strict";(globalThis.webpackChunkwww||=[]).push([[3902],{83523(e,t,n){n.r(t),n.d(t,{assets:()=>l,contentTitle:()=>d,default:()=>p,frontMatter:()=>c,metadata:()=>s,toc:()=>i});const s=JSON.parse('{"id":"reference/client/http-client-base","title":"HttpClientBase","description":"Abstract base class providing the full HTTP verb API and pluggable backend slot shared by all OPRA HTTP client implementations.","source":"@site/docs/reference/client/http-client-base.mdx","sourceDirName":"reference/client","slug":"/reference/client/http-client-base","permalink":"/docs/reference/client/http-client-base","draft":false,"unlisted":false,"tags":[],"version":"current","sidebarPosition":3,"frontMatter":{"title":"HttpClientBase","sidebar_position":3,"description":"Abstract base class providing the full HTTP verb API and pluggable backend slot shared by all OPRA HTTP client implementations."},"sidebar":"docsSidebar","previous":{"title":"HttpRequestObservable","permalink":"/docs/reference/client/http-request-observable"},"next":{"title":"HttpResponse","permalink":"/docs/reference/client/http-response"}}');var r=n(74848),o=n(28453);const c={title:"HttpClientBase",sidebar_position:3,description:"Abstract base class providing the full HTTP verb API and pluggable backend slot shared by all OPRA HTTP client implementations."},d="HttpClientBase",l={},i=[{value:"Constructor",id:"constructor",level:2},{value:"Properties",id:"properties",level:2},{value:"Methods",id:"methods",level:2},{value:"<code>request&lt;TBody&gt;(path, options?)</code>",id:"requesttbodypath-options",level:3},{value:"<code>get&lt;TBody&gt;(path, options?)</code>",id:"gettbodypath-options",level:3},{value:"<code>post&lt;TBody&gt;(path, body, options?)</code>",id:"posttbodypath-body-options",level:3},{value:"<code>patch&lt;TBody&gt;(path, body, options?)</code>",id:"patchtbodypath-body-options",level:3},{value:"<code>put&lt;TBody&gt;(path, body, options?)</code>",id:"puttbodypath-body-options",level:3},{value:"<code>delete&lt;TBody&gt;(path, options?)</code>",id:"deletetbodypath-options",level:3},{value:"<code>fetchDocument(options?)</code>",id:"fetchdocumentoptions",level:3},{value:"<code>bundle(requests)</code>",id:"bundle",level:3},{value:"<code>transaction(requests)</code>",id:"transaction",level:3},{value:"Interfaces",id:"interfaces",level:2},{value:"<code>OpraClientBase.RequestOptions</code>",id:"opraclientbaserequestoptions",level:3},{value:"See also",id:"see-also",level:2}];function a(e){const t={a:"a",code:"code",h1:"h1",h2:"h2",h3:"h3",header:"header",hr:"hr",li:"li",p:"p",pre:"pre",strong:"strong",table:"table",tbody:"tbody",td:"td",th:"th",thead:"thead",tr:"tr",ul:"ul",...(0,o.R)(),...e.components};return(0,r.jsxs)(r.Fragment,{children:[(0,r.jsx)(t.header,{children:(0,r.jsx)(t.h1,{id:"httpclientbase",children:"HttpClientBase"})}),"\n",(0,r.jsxs)(t.p,{children:["Abstract base class for all OPRA HTTP clients. Provides the full request API \u2014 HTTP verb methods, ",(0,r.jsx)(t.code,{children:"fetchDocument"}),", and the pluggable backend slot."]}),"\n",(0,r.jsxs)(t.p,{children:[(0,r.jsx)(t.code,{children:"OpraHttpClient"})," and ",(0,r.jsx)(t.code,{children:"HttpFetchClient"})," both extend ",(0,r.jsx)(t.code,{children:"HttpClientBase"}),". Extend it directly only when you need to inject a custom backend (non-fetch transport)."]}),"\n",(0,r.jsx)(t.pre,{children:(0,r.jsx)(t.code,{className:"language-typescript",children:"import { HttpClientBase, HttpBackend } from '@opra/client';\n\nclass MockClient extends HttpClientBase {\n  constructor() {\n    super(new MockBackend('http://localhost'));\n  }\n}\n"})}),"\n",(0,r.jsx)(t.hr,{}),"\n",(0,r.jsx)(t.h2,{id:"constructor",children:"Constructor"}),"\n",(0,r.jsx)(t.pre,{children:(0,r.jsx)(t.code,{className:"language-typescript",children:"protected constructor(backend: HttpBackend)\n"})}),"\n",(0,r.jsx)(t.p,{children:"The backend is stored internally and used for all requests. Swap it to change the transport layer entirely \u2014 the rest of the client is unchanged."}),"\n",(0,r.jsx)(t.hr,{}),"\n",(0,r.jsx)(t.h2,{id:"properties",children:"Properties"}),"\n",(0,r.jsxs)(t.table,{children:[(0,r.jsx)(t.thead,{children:(0,r.jsxs)(t.tr,{children:[(0,r.jsx)(t.th,{children:"Property"}),(0,r.jsx)(t.th,{children:"Type"}),(0,r.jsx)(t.th,{children:"Description"})]})}),(0,r.jsx)(t.tbody,{children:(0,r.jsxs)(t.tr,{children:[(0,r.jsx)(t.td,{children:(0,r.jsx)(t.code,{children:"serviceUrl"})}),(0,r.jsx)(t.td,{children:(0,r.jsx)(t.code,{children:"string"})}),(0,r.jsx)(t.td,{children:"Base URL from the backend (trailing slash normalized)"})]})})]}),"\n",(0,r.jsx)(t.hr,{}),"\n",(0,r.jsx)(t.h2,{id:"methods",children:"Methods"}),"\n",(0,r.jsx)(t.h3,{id:"requesttbodypath-options",children:(0,r.jsx)(t.code,{children:"request<TBody>(path, options?)"})}),"\n",(0,r.jsxs)(t.p,{children:["Creates a lazy ",(0,r.jsx)(t.code,{children:"HttpRequestObservable"}),". No request is sent until you subscribe or call ",(0,r.jsx)(t.code,{children:".getBody()"})," / ",(0,r.jsx)(t.code,{children:".getResponse()"}),"."]}),"\n",(0,r.jsx)(t.pre,{children:(0,r.jsx)(t.code,{className:"language-typescript",children:"request<TBody = any>(\n  path: string,\n  options?: OpraClientBase.RequestOptions,\n): HttpRequestObservable<TBody, TBody, TRequestOptions, TResponseExt>\n"})}),"\n",(0,r.jsx)(t.h3,{id:"gettbodypath-options",children:(0,r.jsx)(t.code,{children:"get<TBody>(path, options?)"})}),"\n",(0,r.jsx)(t.pre,{children:(0,r.jsx)(t.code,{className:"language-typescript",children:"get<TBody = any>(\n  path: string,\n  options?: Omit<OpraClientBase.RequestOptions, 'method' | 'body'>,\n): HttpRequestObservable<TBody, ...>\n"})}),"\n",(0,r.jsx)(t.h3,{id:"posttbodypath-body-options",children:(0,r.jsx)(t.code,{children:"post<TBody>(path, body, options?)"})}),"\n",(0,r.jsx)(t.pre,{children:(0,r.jsx)(t.code,{className:"language-typescript",children:"post<TBody = any>
1(path: string, body: any, options?): HttpRequestObservable<TBody, ...>\n"})}),"\n",(0,r.jsx)(t.h3,{id:"patchtbodypath-body-options",children:(0,r.jsx)(t.code,{children:"patch<TBody>(path, body, options?)"})}),"\n",(0,r.jsx)(t.pre,{children:(0,r.jsx)(t.code,{className:"language-typescript",children:"patch<TBody = any>(path: string, body: any, options?): HttpRequestObservable<TBody, ...>\n"})}),"\n",(0,r.jsx)(t.h3,{id:"puttbodypath-body-options",children:(0,r.jsx)(t.code,{children:"put<TBody>(path, body, options?)"})}),"\n",(0,r.jsx)(t.pre,{children:(0,r.jsx)(t.code,{className:"language-typescript",children:"put<TBody = any>(path: string, body: any, options?): HttpRequestObservable<TBody, ...>\n"})}),"\n",(0,r.jsx)(t.h3,{id:"deletetbodypath-options",children:(0,r.jsx)(t.code,{children:"delete<TBody>(path, options?)"})}),"\n",(0,r.jsx)(t.pre,{children:(0,r.jsx)(t.code,{className:"language-typescript",children:"delete<TBody = any>(\n  path: string,\n  options?: Omit<OpraClientBase.RequestOptions, 'method' | 'body'>,\n): HttpRequestObservable<TBody, ...>\n"})}),"\n",(0,r.jsx)(t.h3,{id:"fetchdocumentoptions",children:(0,r.jsx)(t.code,{children:"fetchDocument(options?)"})}),"\n",(0,r.jsxs)(t.p,{children:["Fetches the OPRA API schema from ",(0,r.jsx)(t.code,{children:"$schema"})," on the service and returns a parsed ",(0,r.jsx)(t.code,{children:"ApiDocument"}),". Resolves referenced sub-documents recursively."]}),"\n",(0,r.jsx)(t.pre,{children:(0,r.jsx)(t.code,{className:"language-typescript",children:"fetchDocument(options?: { documentId?: string }): Promise<ApiDocument>\n"})}),"\n",(0,r.jsx)(t.hr,{}),"\n",(0,r.jsx)(t.h3,{id:"bundle",children:(0,r.jsx)(t.code,{children:"bundle(requests)"})}),"\n",(0,r.jsxs)(t.p,{children:["Groups multiple ",(0,r.jsx)(t.code,{children:"HttpRequestObservable"})," instances into a single ",(0,r.jsx)(t.code,{children:"multipart/mixed"})," batch request sent to ",(0,r.jsx)(t.code,{children:"POST /$bundle"}),". Returns an ",(0,r.jsx)(t.code,{children:"HttpBundleObservable"})," whose ",(0,r.jsx)(t.code,{children:"getResponses()"})," resolves with the array of sub-responses in input order."]}),"\n",(0,r.jsx)(t.pre,{children:(0,r.jsx)(t.code,{className:"language-typescript",children:"bundle(requests: HttpRequestObservable<any>[]): HttpBundleObservable\n"})}),"\n",(0,r.jsxs)(t.p,{children:["Requests passed to ",(0,r.jsx)(t.code,{children:"bundle()"})," ",(0,r.jsx)(t.strong,{children:"do not fire their own network calls"})," \u2014 they are serialized as parts of the bundle. After the bundle resolves, each individual request can still be awaited via ",(0,r.jsx)(t.code,{children:".getBody()"})," / ",(0,r.jsx)(t.code,{children:".getResponse()"})," (served from a cached ",(0,r.jsx)(t.code,{children:"ReplaySubject"}),")."]}),"\n",(0,r.jsx)(t.pre,{children:(0,r.jsx)(t.code,{className:"language-typescript",children:"const r1 = client.get<Customer>('customers@1');\nconst r2 = client.get<Order[]>('orders');\n\nconst [res1, res2] = await client.bundle([r1, r2]).getResponses();\n\n// Individual awaits also work \u2014 resolved from the bundle response:\nconst customer = await r1.getBody();\nconst orders   = await r2.getBody();\n"})}),"\n",(0,r.jsxs)(t.p,{children:["\u2192 ",(0,r.jsx)(t.a,{href:"/docs/reference/client/http-bundle-observable",children:(0,r.jsx)(t.code,{children:"HttpBundleObservable"})})]}),"\n",(0,r.jsx)(t.hr,{}),"\n",(0,r.jsx)(t.h3,{id:"transaction",children:(0,r.jsx)(t.code,{children:"transaction(requests)"})}),"\n",(0,r.jsxs)(t.p,{children:["Same as ",(0,r.jsx)(t.code,{children:"bundle()"}),", but appends ",(0,r.jsx)(t.code,{children:"?transaction=true"})," to the bundle URL. The server wraps all sub-requests in a single database transaction \u2014 committing on full success or rolling back on any failure."]}),"\n",(0,r.jsx)(t.pre,{children:(0,r.jsx)(t.code,{className:"language-typescript",children:"transaction(requests: HttpRequestObservable<any>[]): HttpBundleObservable\n"})}),"\n",(0,r.jsx)(t.pre,{children:(0,r.jsx)(t.code,{className:"language-typescript",children:"const r1 = client.post('orders', { productId: 'p1', qty: 2 });\nconst r2 = client.patch('inventory/p1', { reserve: 2 });\n\nawait client.transaction([r1, r2]).getResponses();\n// Both ops commit together \u2014 or both roll back.\n"})}),"\n",(0,r.jsxs)(t.p,{children:["Supported server-side by ",(0,r.jsx)(t.code,{children:"@opra/mongodb"})," and ",(0,r.jsx)(t.code,{children:"@opra/sqb"}),". The ",(0,r.jsx)(t.code,{children:"?transaction=true"})," query parameter is set automatically \u2014 no extra configuration needed."]}),"\n",(0,r.jsxs)(t.p,{children:["\u2192 ",(0,r.jsx)(t.a,{href:"/docs/reference/client/http-bundle-observable",children:(0,r.jsx)(t.code,{children:"HttpBundleObservable"})})," \xb7 ",(0,r.jsx)(t.a,{href:"/docs/protocols/http/batch-requests#transactions",children:"Transactions"})]}),"\n",(0,r.jsx)(t.hr,{}),"\n",(0,r.jsx)(t.h2,{id:"interfaces",children:"Interfaces"}),"\n",(0,r.jsx)(t.h3,{id:"opraclientbaserequestoptions",children:(0,r.jsx)(t.code,{children:"OpraClientBase.RequestOptions"})}),"\n",(0,r.jsx)(t.pre,{children:(0,r.jsx)(t.code,{className:"language-typescript",children:"type RequestOptions = Partial<Omit<HttpBackend.RequestInit, 'url'>> & {\n  params?: URLSearchParamsInit;\n};\n"})}),"\n",(0,r.jsx)(t.hr,{}),"\n",(0,r.jsx)(t.h2,{id:"see-also",children:"See also"}),"\n",(0,r.jsxs)(t.ul,{children:["\n",(0,r.jsxs)(t.li,{children:[(0,r.jsx)(t.a,{href:"/docs/reference/client/http-client",children:(0,r.jsx)(t.code,{children:"OpraHttpCl
1ient"})})," \u2014 concrete implementation with ",(0,r.jsx)(t.code,{children:"FetchBackend"})]}),"\n",(0,r.jsxs)(t.li,{children:[(0,r.jsx)(t.a,{href:"/docs/reference/client/http-backend",children:(0,r.jsx)(t.code,{children:"HttpBackend"})})," \u2014 abstract transport interface"]}),"\n",(0,r.jsxs)(t.li,{children:[(0,r.jsx)(t.a,{href:"/docs/reference/client/http-request-observable",children:(0,r.jsx)(t.code,{children:"HttpRequestObservable"})})," \u2014 fluent builder returned by every method"]}),"\n"]})]})}function p(e={}){const{wrapper:t}={...(0,o.R)(),...e.components};return t?(0,r.jsx)(t,{...e,children:(0,r.jsx)(a,{...e})}):a(e)}},28453(e,t,n){n.d(t,{R:()=>c,x:()=>d});var s=n(96540);const r={},o=s.createContext(r);function c(e){const t=s.useContext(o);return s.useMemo(function(){return"function"==typeof e?e(t):{...t,...e}},[t,e])}function d(e){let t;return t=e.disableParentContext?"function"==typeof e.components?e.components(r):e.components||r:c(e.components),s.createElement(o.Provider,{value:t},e.children)}}}]);

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.