Skip to content

Commit a7cc3e1

Browse files
committed
docs(readme): document how to make undocumented requests (#730)
1 parent 3c59fa7 commit a7cc3e1

File tree

1 file changed

+48
-2
lines changed

1 file changed

+48
-2
lines changed

README.md

+48-2
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,51 @@ console.log(raw.headers.get('X-My-Header'));
437437
console.log(chatCompletion);
438438
```
439439

440-
## Customizing the fetch client
440+
### Making custom/undocumented requests
441+
442+
This library is typed for convenient access to the documented API. If you need to access undocumented
443+
endpoints, params, or response properties, the library can still be used.
444+
445+
#### Undocumented endpoints
446+
447+
To make requests to undocumented endpoints, you can use `client.get`, `client.post`, and other HTTP verbs.
448+
Options on the client, such as retries, will be respected when making these requests.
449+
450+
```ts
451+
await client.post('/some/path', {
452+
body: { some_prop: 'foo' },
453+
query: { some_query_arg: 'bar' },
454+
});
455+
```
456+
457+
#### Undocumented params
458+
459+
To make requests using undocumented parameters, you may use `// @ts-expect-error` on the undocumented
460+
parameter. This library doesn't validate at runtime that the request matches the type, so any extra values you
461+
send will be sent as-is.
462+
463+
```ts
464+
client.foo.create({
465+
foo: 'my_param',
466+
bar: 12,
467+
// @ts-expect-error baz is not yet public
468+
baz: 'undocumented option',
469+
});
470+
```
471+
472+
For requests with the `GET` verb, any extra params will be in the query, all other requests will send the
473+
extra param in the body.
474+
475+
If you want to explicitly send an extra argument, you can do so with the `query`, `body`, and `headers` request
476+
options.
477+
478+
#### Undocumented properties
479+
480+
To access undocumented response properties, you may access the response object with `// @ts-expect-error` on
481+
the response object, or cast the response object to the requisite type. Like the request params, we do not
482+
validate or strip extra properties from the response from the API.
483+
484+
### Customizing the fetch client
441485

442486
By default, this library uses `node-fetch` in Node, and expects a global `fetch` function in other environments.
443487

@@ -455,6 +499,8 @@ import OpenAI from 'openai';
455499
To do the inverse, add `import "openai/shims/node"` (which does import polyfills).
456500
This can also be useful if you are getting the wrong TypeScript types for `Response` ([more details](https://github.com/openai/openai-node/tree/master/src/_shims#readme)).
457501

502+
### Logging and middleware
503+
458504
You may also provide a custom `fetch` function when instantiating the client,
459505
which can be used to inspect or alter the `Request` or `Response` before/after each request:
460506

@@ -475,7 +521,7 @@ const client = new OpenAI({
475521
Note that if given a `DEBUG=true` environment variable, this library will log all requests and responses automatically.
476522
This is intended for debugging purposes only and may change in the future without notice.
477523

478-
## Configuring an HTTP(S) Agent (e.g., for proxies)
524+
### Configuring an HTTP(S) Agent (e.g., for proxies)
479525

480526
By default, this library uses a stable agent for all http/https requests to reuse TCP connections, eliminating many TCP & TLS handshakes and shaving around 100ms off most requests.
481527

0 commit comments

Comments
 (0)