Skip to content

Rename all methods to UPPERCASE #1243

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/witty-ears-change.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"openapi-fetch": minor
---

⚠️ Breaking: rename all methods to UPPERCASE (`GET()`, `POST()`, etc.)
6 changes: 3 additions & 3 deletions docs/src/content/docs/openapi-fetch/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ createClient<paths>(options);

## Fetch options

The following options apply to all request methods (`.get()`, `.post()`, etc.)
The following options apply to all request methods (`.GET()`, `.POST()`, etc.)

```ts
client.get("/my-url", options);
Expand All @@ -43,7 +43,7 @@ client.get("/my-url", options);
This library uses <a href="https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams" target="_blank" rel="noopener noreferrer">URLSearchParams</a> to <a href="https://swagger.io/docs/specification/serialization/" target="_blank" rel="noopener noreferrer">serialize query parameters</a>. For complex query param types (e.g. arrays) you’ll need to provide your own `querySerializer()` method that transforms query params into a URL-safe string:

```ts
const { data, error } = await get("/search", {
const { data, error } = await GET("/search", {
params: {
query: { tags: ["food", "california", "healthy"] },
},
Expand All @@ -66,7 +66,7 @@ const { data, error } = await get("/search", {
Similar to [querySerializer](#querySerializer), bodySerializer works for requestBody. You probably only need this when using `multipart/form-data`:

```ts
const { data, error } = await put("/submit", {
const { data, error } = await PUT("/submit", {
body: {
name: "",
query: { version: 2 },
Expand Down
10 changes: 5 additions & 5 deletions docs/src/content/docs/openapi-fetch/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ export const client = computed(authToken, (currentToken) =>
// src/some-other-file.ts
import { client } from "./lib/api";

const { get, post } = client.get();
const { GET, POST } = client.get();

get("/some-authenticated-url", {
GET("/some-authenticated-url", {
/* … */
});
```
Expand Down Expand Up @@ -63,7 +63,7 @@ export default new Proxy(baseClient, {
// src/some-other-file.ts
import client from "./lib/api";

client.get("/some-authenticated-url", {
client.GET("/some-authenticated-url", {
/* … */
});
```
Expand All @@ -89,7 +89,7 @@ export default createClient({
// src/some-other-file.ts
import client from "./lib/api";

client.get("/my/endpoint", {
client.GET("/my/endpoint", {
/* … */
});
```
Expand Down Expand Up @@ -202,7 +202,7 @@ function useUser({ params, body, reactQuery }: UseQueryOptions<paths[typeof GET_
// add any other hook dependencies here
],
queryFn: async ({ signal }) => {
const { data, error } = await client.get(GET_USER, {
const { data, error } = await client.GET(GET_USER, {
params,
// body - isn’t used for GET, but needed for other request types
signal, // allows React Query to cancel request
Expand Down
18 changes: 9 additions & 9 deletions docs/src/content/docs/openapi-fetch/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@ The syntax is inspired by popular libraries like react-query or Apollo client, b
import createClient from "openapi-fetch";
import { paths } from "./v1"; // generated from openapi-typescript

const { get, put } = createClient<paths>({ baseUrl: "https://myapi.dev/v1/" });
const { GET, PUT } = createClient<paths>({ baseUrl: "https://myapi.dev/v1/" });

// Type-checked request
await put("/blogposts", {
await PUT("/blogposts", {
body: {
title: "My New Post",
// ❌ Property 'publish_date' is missing in type …
},
});

// Type-checked response
const { data, error } = await get("/blogposts/{post_id}", { params: { path: { post_id: "123" } } });
const { data, error } = await GET("/blogposts/{post_id}", { params: { path: { post_id: "123" } } });
console.log(data.title); // ❌ 'data' is possibly 'undefined'
console.log(error.message); // ❌ 'error' is possibly 'undefined'
console.log(data?.foo); // ❌ Property 'foo' does not exist on type …
Expand Down Expand Up @@ -90,16 +90,16 @@ Here’s how you’d fetch GET `/blogposts/{post_id}` and PUT `/blogposts`:
import createClient from "openapi-fetch";
import { paths } from "./v1";

const { get, put } = createClient<paths>({ baseUrl: "https://myapi.dev/v1/" });
const { GET, PUT } = createClient<paths>({ baseUrl: "https://myapi.dev/v1/" });

const { data, error } = await get("/blogposts/{post_id}", {
const { data, error } = await GET("/blogposts/{post_id}", {
params: {
path: { post_id: "my-post" },
query: { version: 2 },
},
});

const { data, error } = await put("/blogposts", {
const { data, error } = await PUT("/blogposts", {
body: {
title: "New Post",
body: "<p>New post body</p>",
Expand All @@ -110,7 +110,7 @@ const { data, error } = await put("/blogposts", {

### Pathname

The pathname of `get()`, `put()`, `post()`, etc. **must match your schema literally.** Note in the example, the URL is `/blogposts/{post_id}`. This library will replace all `path` params for you (so they can be typechecked)
The pathname of `GET()`, `PUT()`, `POST()`, etc. **must match your schema literally.** Note in the example, the URL is `/blogposts/{post_id}`. This library will replace all `path` params for you (so they can be typechecked)

> ✨ **Tip**
>
Expand All @@ -121,9 +121,9 @@ The pathname of `get()`, `put()`, `post()`, etc. **must match your schema litera

### Request

The `get()` request shown needed the `params` object that groups <a href="https://spec.openapis.org/oas/latest.html#parameter-object" target="_blank" rel="noopener noreferrer">parameters by type</a> (`path` or `query`). If a required param is missing, or the wrong type, a type error will be thrown.
The `GET()` request shown needed the `params` object that groups <a href="https://spec.openapis.org/oas/latest.html#parameter-object" target="_blank" rel="noopener noreferrer">parameters by type</a> (`path` or `query`). If a required param is missing, or the wrong type, a type error will be thrown.

The `post()` request required a `body` object that provided all necessary <a href="https://spec.openapis.org/oas/latest.html#request-body-object" target="_blank" rel="noopener noreferrer">requestBody</a> data.
The `POST()` request required a `body` object that provided all necessary <a href="https://spec.openapis.org/oas/latest.html#request-body-object" target="_blank" rel="noopener noreferrer">requestBody</a> data.

### Response

Expand Down
12 changes: 6 additions & 6 deletions packages/openapi-fetch/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@ The syntax is inspired by popular libraries like react-query or Apollo client, b
import createClient from "openapi-fetch";
import { paths } from "./v1"; // (generated from openapi-typescript)

const { get, post } = createClient<paths>({ baseUrl: "https://myapi.dev/v1/" });
const { GET, POST } = createClient<paths>({ baseUrl: "https://myapi.dev/v1/" });

// Type-checked request
await post("/create-post", {
await POST("/create-post", {
body: {
title: "My New Post",
// ❌ Property 'publish_date' is missing in type …
},
});

// Type-checked response
const { data, error } = await get("/blogposts/my-blog-post");
const { data, error } = await GET("/blogposts/my-blog-post");

console.log(data.title); // ❌ 'data' is possibly 'undefined'
console.log(error.message); // ❌ 'error' is possibly 'undefined'
Expand Down Expand Up @@ -86,16 +86,16 @@ Here’s how you’d fetch GET `/blogposts/{post_id}` and PUT `/blogposts`:
import createClient from "openapi-fetch";
import { paths } from "./v1";

const { get, put } = createClient<paths>({ baseUrl: "https://myapi.dev/v1/" });
const { GET, PUT } = createClient<paths>({ baseUrl: "https://myapi.dev/v1/" });

const { data, error } = await get("/blogposts/{post_id}", {
const { data, error } = await GET("/blogposts/{post_id}", {
params: {
path: { post_id: "my-post" },
query: { version: 2 },
},
});

const { data, error } = await put("/blogposts", {
const { data, error } = await PUT("/blogposts", {
body: {
title: "New Post",
body: "<p>New post body</p>",
Expand Down
Loading