Skip to content

Commit

Permalink
Better result typing (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
zemd authored Aug 23, 2024
1 parent f5123a3 commit 01691d6
Show file tree
Hide file tree
Showing 28 changed files with 274 additions and 120 deletions.
5 changes: 5 additions & 0 deletions .changeset/cold-pandas-exist.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@zemd/http-client": major
---

Make it possible to map and validate response, and make typescript to highlight proper types
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ This repository contains a collection of libs that help to work with different A
of api clients.

At the moment there are two main libraries:
- [`@zemd/http-client`](./packages/http-client) - a lib which follows simple concept for building http clients
- [`@zemd/figma-rest-api`](./apis/figma) - a lib that is built on top of `@zemd/http-client` and provides a client for working with Figma REST API.

## License
- [`@zemd/http-client`](./packages/http-client) - a lib which follows simple concept for building http clients
- [`@zemd/figma-rest-api`](./apis/figma) - a lib that is built on top of `@zemd/http-client` and provides a client for working with Figma REST API.

## License

All the code in the repository released under the Apache 2.0 license

## Donate

[![](https://img.shields.io/badge/patreon-donate-yellow.svg)](https://www.patreon.com/red_rabbit)
[![](https://img.shields.io/static/v1?label=UNITED24&message=support%20Ukraine&color=blue)](https://u24.gov.ua/)

2 changes: 0 additions & 2 deletions apis/figma/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ some experimental features. In this case, you can construct your own api call us

Since the library is built on top of `@zemd/http-client` you can compose different configurations together.


## License

`@zemd/figma-rest-api` released under the Apache 2.0 license
Expand All @@ -37,4 +36,3 @@ Since the library is built on top of `@zemd/http-client` you can compose differe

[![](https://img.shields.io/badge/patreon-donate-yellow.svg)](https://www.patreon.com/red_rabbit)
[![](https://img.shields.io/static/v1?label=UNITED24&message=support%20Ukraine&color=blue)](https://u24.gov.ua/)

6 changes: 3 additions & 3 deletions apis/figma/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@
},
"devDependencies": {
"@types/bun": "latest",
"@zemd/tsconfig": "^1.2.0",
"typescript": "^5.3.3"
"@zemd/tsconfig": "^1.3.0",
"typescript": "^5.5.4"
},
"dependencies": {
"@zemd/http-client": "^1.0.0",
"zod": "^3.22.4"
"zod": "^3.23.8"
}
}
14 changes: 7 additions & 7 deletions apis/figma/src/api/comments.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { z } from "zod";
import { body, method, query, type TEndpointDec } from "@zemd/http-client";
import { body, method, query, type TEndpointDecTuple } from "@zemd/http-client";

const GetCommentsQuerySchema = z.object({
as_md: z.coerce.boolean().optional(),
Expand All @@ -10,7 +10,7 @@ export interface GetCommentsQuery extends z.infer<typeof GetCommentsQuerySchema>
/**
* Gets a list of comments left on the file.
*/
export const getComments = (key: string, options?: GetCommentsQuery): TEndpointDec => {
export const getComments = (key: string, options?: GetCommentsQuery): TEndpointDecTuple => {
const transformers = [method("GET")];
if (options) {
transformers.push(query(GetCommentsQuerySchema.passthrough().parse(options)));
Expand Down Expand Up @@ -57,7 +57,7 @@ export interface PostCommentsQuery extends z.infer<typeof PostCommentsQuerySchem
/**
* Posts a new comment on the file.
*/
export const postComments = (key: string, message: PostCommentsQuery): TEndpointDec => {
export const postComments = (key: string, message: PostCommentsQuery): TEndpointDecTuple => {
return [
`/v1/files/${key}/comments`,
[method("POST"), body(JSON.stringify(PostCommentsQuerySchema.passthrough().parse(message)))],
Expand All @@ -67,7 +67,7 @@ export const postComments = (key: string, message: PostCommentsQuery): TEndpoint
/**
* Deletes a specific comment. Only the person who made the comment is allowed to delete it.
*/
export const deleteComments = (key: string, commendId: string): TEndpointDec => {
export const deleteComments = (key: string, commendId: string): TEndpointDecTuple => {
return [`/v1/files/${key}/comments/${commendId}`, [method("DELETE")]];
};

Expand All @@ -84,7 +84,7 @@ export const getCommentsReactions = (
key: string,
commentId: string,
options?: GetCommentsReactionsQuery,
): TEndpointDec => {
): TEndpointDecTuple => {
const transformers = [method("GET")];
if (options) {
transformers.push(query(GetCommentsReactionsQuerySchema.passthrough().parse(options)));
Expand All @@ -105,7 +105,7 @@ export const postCommentsReactions = (
key: string,
commentId: string,
options: PostCommentsReactionsQuery,
): TEndpointDec => {
): TEndpointDecTuple => {
return [
`/v1/files/${key}/comments/${commentId}/reactions`,
[method("POST"), body(JSON.stringify(PostCommentsReactionsQuerySchema.passthrough().parse(options)))],
Expand All @@ -120,7 +120,7 @@ export const deleteCommentsReactions = (
key: string,
commentId: string,
options: PostCommentsReactionsQuery,
): TEndpointDec => {
): TEndpointDecTuple => {
return [
`/v1/files/${key}/comments/${commentId}/reactions`,
[method("DELETE"), query(PostCommentsReactionsQuerySchema.passthrough().parse(options))],
Expand Down
20 changes: 10 additions & 10 deletions apis/figma/src/api/components.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { z } from "zod";
import { method, query, type TEndpointDec } from "@zemd/http-client";
import { method, query, type TEndpointDecTuple } from "@zemd/http-client";

export const PaginationQuerySchema = z.object({
page_size: z.number().optional(),
Expand All @@ -12,29 +12,29 @@ export interface GetTeamComponentsQuery extends z.infer<typeof PaginationQuerySc
/**
* Get a paginated list of published components within a team library
*/
export const getTeamComponents = (teamId: string, options: GetTeamComponentsQuery): TEndpointDec => {
export const getTeamComponents = (teamId: string, options: GetTeamComponentsQuery): TEndpointDecTuple => {
return [`/v1/teams/${teamId}/components`, [method("GET"), query(PaginationQuerySchema.passthrough().parse(options))]];
};

/**
* Get a list of published components within a file library
*/
export const getFileComponents = (key: string): TEndpointDec => {
export const getFileComponents = (key: string): TEndpointDecTuple => {
return [`/v1/files/${key}/components`, [method("GET")]];
};

/**
* Get metadata on a component by key.
*/
export const getComponent = (key: string): TEndpointDec => {
export const getComponent = (key: string): TEndpointDecTuple => {
return [`/v1/components/${key}`, [method("GET")]];
};

export interface GetTeamComponentSetsQuery extends z.infer<typeof PaginationQuerySchema> {}
/**
* Get a paginated list of published component sets within a team library
*/
export const getTeamComponentSets = (teamId: string, options: GetTeamComponentSetsQuery): TEndpointDec => {
export const getTeamComponentSets = (teamId: string, options: GetTeamComponentSetsQuery): TEndpointDecTuple => {
return [
`/v1/teams/${teamId}/component_sets`,
[method("GET"), query(PaginationQuerySchema.passthrough().parse(options))],
Expand All @@ -44,14 +44,14 @@ export const getTeamComponentSets = (teamId: string, options: GetTeamComponentSe
/**
* Get a list of published component sets within a file library
*/
export const getFileComponentSets = (key: string): TEndpointDec => {
export const getFileComponentSets = (key: string): TEndpointDecTuple => {
return [`/v1/files/${key}/component_sets`, [method("GET")]];
};

/**
* Get metadata on a component set by key.
*/
export const getComponentSet = (key: string): TEndpointDec => {
export const getComponentSet = (key: string): TEndpointDecTuple => {
return [`/v1/component_sets/${key}`, [method("GET")]];
};

Expand All @@ -60,20 +60,20 @@ export interface GetTeamStylesQuery extends z.infer<typeof PaginationQuerySchema
/**
* Get a paginated list of published styles within a team library
*/
export const getTeamStyles = (teamId: string, options: GetTeamStylesQuery): TEndpointDec => {
export const getTeamStyles = (teamId: string, options: GetTeamStylesQuery): TEndpointDecTuple => {
return [`/v1/teams/${teamId}/styles`, [method("GET"), query(PaginationQuerySchema.passthrough().parse(options))]];
};

/**
* Get a list of published styles within a file library
*/
export const getFileStyles = (key: string): TEndpointDec => {
export const getFileStyles = (key: string): TEndpointDecTuple => {
return [`/v1/files/${key}/styles`, [method("GET")]];
};

/**
* Get metadata on a style by key.
*/
export const getStyle = (key: string): TEndpointDec => {
export const getStyle = (key: string): TEndpointDecTuple => {
return [`/v1/styles/${key}`, [method("GET")]];
};
10 changes: 5 additions & 5 deletions apis/figma/src/api/dev_resources.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { z } from "zod";
import { body, method, query, type TEndpointDec } from "@zemd/http-client";
import { body, method, query, type TEndpointDecTuple } from "@zemd/http-client";

export const GetDevResourcesQuerySchema = z.object({
node_ids: z.string().optional(),
Expand All @@ -10,7 +10,7 @@ export interface GetDevResourcesQuery extends z.infer<typeof GetDevResourcesQuer
/**
* Get dev resources in a file.
*/
export const getDevResources = (key: string, options?: GetDevResourcesQuery): TEndpointDec => {
export const getDevResources = (key: string, options?: GetDevResourcesQuery): TEndpointDecTuple => {
const transformers = [method("GET")];
if (options) {
transformers.push(query(GetDevResourcesQuerySchema.passthrough().parse(options)));
Expand Down Expand Up @@ -46,7 +46,7 @@ export interface PostDevResources extends z.infer<typeof PostDevResourcesBodySch
* - The node already has the maximum of 10 dev resources.
* - Another dev resource for the node has the same url.
*/
export const postDevResources = (options: PostDevResources): TEndpointDec => {
export const postDevResources = (options: PostDevResources): TEndpointDecTuple => {
return [
`/v1/dev_resources`,
[method("POST"), body(JSON.stringify(PostDevResourcesBodySchema.passthrough().parse(options)))],
Expand Down Expand Up @@ -74,7 +74,7 @@ export interface PutDevResourcesBody extends z.infer<typeof PutDevResourcesBodyS
* If there are any dev resources that cannot be updated, you may still
* get a 200 response. These resources will show up in the errors array.
*/
export const putDevResources = (options: PutDevResourcesBody): TEndpointDec => {
export const putDevResources = (options: PutDevResourcesBody): TEndpointDecTuple => {
return [
`/v1/dev_resources`,
[method("PUT"), body(JSON.stringify(PutDevResourcesBodySchema.passthrough().parse(options)))],
Expand All @@ -84,6 +84,6 @@ export const putDevResources = (options: PutDevResourcesBody): TEndpointDec => {
/**
* Delete a dev resources from a file.
*/
export const deleteDevResources = (key: string, devResourceId: string): TEndpointDec => {
export const deleteDevResources = (key: string, devResourceId: string): TEndpointDecTuple => {
return [`/v1/files/${key}/dev_resources/${devResourceId}`, [method("DELETE")]];
};
10 changes: 5 additions & 5 deletions apis/figma/src/api/files.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { method, query, type TEndpointDec } from "@zemd/http-client";
import { method, query, type TEndpointDecTuple } from "@zemd/http-client";
import { z } from "zod";

export const GetFileQuerySchema = z.object({
Expand All @@ -23,7 +23,7 @@ export interface GetFileQuery extends z.infer<typeof GetFileQuerySchema> {}
* The components key contains a mapping from node IDs to component metadata.
* This is to help you determine which components each instance comes from.
*/
export const getFile = (key: string, options?: GetFileQuery): TEndpointDec => {
export const getFile = (key: string, options?: GetFileQuery): TEndpointDecTuple => {
const transformers = [method("GET")];

if (options) {
Expand Down Expand Up @@ -66,7 +66,7 @@ export interface GetFileNodesQuery extends z.infer<typeof GetFileNodesQuerySchem
* Important: the nodes map may contain values that are null . This may be due
* to the node id not existing within the specified file.
*/
export const getFileNodes = (key: string, options: GetFileNodesQuery): TEndpointDec => {
export const getFileNodes = (key: string, options: GetFileNodesQuery): TEndpointDecTuple => {
return [`/files/${key}/nodes`, [method("GET"), query(GetFileNodesQuerySchema.passthrough().parse(options))]];
};

Expand Down Expand Up @@ -103,7 +103,7 @@ export interface GetImageQuery extends z.infer<typeof GetImageQuerySchema> {}
* To render multiple images from the same file, use the ids query parameter
* to specify multiple node ids.
*/
export const getImage = (key: string, options: GetImageQuery): TEndpointDec => {
export const getImage = (key: string, options: GetImageQuery): TEndpointDecTuple => {
return [`/images/${key}`, [method("GET"), query(GetImageQuerySchema.passthrough().parse(options))]];
};

Expand All @@ -119,6 +119,6 @@ export const getImage = (key: string, options: GetImageQuery): TEndpointDec => {
* references are located in the output of the GET files endpoint under the imageRef
* attribute in a Paint.
*/
export const getImageFills = (key: string): TEndpointDec => {
export const getImageFills = (key: string): TEndpointDecTuple => {
return [`/files/${key}/images`, [method("GET")]];
};
4 changes: 2 additions & 2 deletions apis/figma/src/api/logs.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { z } from "zod";
import { method, query, type TEndpointDec } from "@zemd/http-client";
import { method, query, type TEndpointDecTuple } from "@zemd/http-client";

export const GetActivityLogsQuerySchema = z.object({
events: z.string().optional(),
Expand All @@ -14,7 +14,7 @@ export interface GetActivityLogsQuery extends z.infer<typeof GetActivityLogsQuer
/**
* Returns a list of activity log events
*/
export const getActivityLogs = (options?: GetActivityLogsQuery): TEndpointDec => {
export const getActivityLogs = (options?: GetActivityLogsQuery): TEndpointDecTuple => {
const transformers = [method("GET")];
if (options) {
transformers.push(query(GetActivityLogsQuerySchema.passthrough().parse(options)));
Expand Down
4 changes: 2 additions & 2 deletions apis/figma/src/api/payments.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { z } from "zod";
import { method, query, type TEndpointDec } from "@zemd/http-client";
import { method, query, type TEndpointDecTuple } from "@zemd/http-client";

export const GetPaymentsQuerySchema = z.object({
user_id: z.number(),
Expand All @@ -13,6 +13,6 @@ export interface GetPaymentsQuery extends z.infer<typeof GetPaymentsQuerySchema>
/**
* Fetch the payment information of a user on your resource using IDs.
*/
export const getPayments = (options: GetPaymentsQuery): TEndpointDec => {
export const getPayments = (options: GetPaymentsQuery): TEndpointDecTuple => {
return [`/v1/payments`, [method("GET"), query(GetPaymentsQuerySchema.passthrough().parse(options))]];
};
6 changes: 3 additions & 3 deletions apis/figma/src/api/projects.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { z } from "zod";
import { method, query, type TEndpointDec } from "@zemd/http-client";
import { method, query, type TEndpointDecTuple } from "@zemd/http-client";

/**
* You can use this Endpoint to get a list of all the Projects within
Expand All @@ -10,7 +10,7 @@ import { method, query, type TEndpointDec } from "@zemd/http-client";
* team you are a part of. The team id will be present in the URL after
* the word team and before your team name.
*/
export const getTeamProjects = (teamId: string): TEndpointDec => {
export const getTeamProjects = (teamId: string): TEndpointDecTuple => {
return [`/v1/teams/${teamId}/projects`, [method("GET")]];
};

Expand All @@ -23,7 +23,7 @@ export interface GetProjectFilesQuery extends z.infer<typeof GetProjectFilesQuer
/**
* List the files in a given project.
*/
export const getProjectFiles = (projectId: string, options?: GetProjectFilesQuery): TEndpointDec => {
export const getProjectFiles = (projectId: string, options?: GetProjectFilesQuery): TEndpointDecTuple => {
const transformers = [method("GET")];
if (options) {
transformers.push(query(GetProjectFilesQuerySchema.passthrough().parse(options)));
Expand Down
4 changes: 2 additions & 2 deletions apis/figma/src/api/users.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { method, type TEndpointDec } from "@zemd/http-client";
import { method, type TEndpointDecTuple } from "@zemd/http-client";

/**
* If you are using OAuth for authentication, this endpoint can be
* used to get user information for the authenticated user.
*/
export const getMe = (): TEndpointDec => {
export const getMe = (): TEndpointDecTuple => {
return [`/v1/me`, [method("GET")]];
};
8 changes: 4 additions & 4 deletions apis/figma/src/api/variables.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { z } from "zod";
import { body, method, type TEndpointDec } from "@zemd/http-client";
import { body, method, type TEndpointDecTuple } from "@zemd/http-client";
import { ColorProp, VariableAliasProp } from "../schema.js";

/**
Expand All @@ -19,7 +19,7 @@ import { ColorProp, VariableAliasProp } from "../schema.js";
* Instead, you will need to use the GET /v1/files/:file_key/variables/local
* endpoint, in the same file, to examine the mode values.
*/
export const getLocalVariables = (key: string): TEndpointDec => {
export const getLocalVariables = (key: string): TEndpointDecTuple => {
return [`/v1/files/${key}/variables/local`, [method("GET")]];
};

Expand All @@ -45,7 +45,7 @@ export const getLocalVariables = (key: string): TEndpointDec => {
* change to a variable was published. For variable collections, this timestamp will
* change any time a variable in the collection is changed.
*/
export const getPublishedVariables = (key: string): TEndpointDec => {
export const getPublishedVariables = (key: string): TEndpointDecTuple => {
return [`/v1/files/${key}/variables/published`, [method("GET")]];
};

Expand Down Expand Up @@ -170,7 +170,7 @@ export interface PostVariablesBody extends z.infer<typeof PostVariablesBodySchem
/**
*
*/
export const postVariables = (key: string, options?: PostVariablesBody): TEndpointDec => {
export const postVariables = (key: string, options?: PostVariablesBody): TEndpointDecTuple => {
const transformers = [method("POST")];
if (options) {
transformers.push(body(JSON.stringify(PostVariablesBodySchema.passthrough().parse(options))));
Expand Down
4 changes: 2 additions & 2 deletions apis/figma/src/api/versions.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { TEndpointDec } from "@zemd/http-client";
import type { TEndpointDecTuple } from "@zemd/http-client";

/**
* A list of the versions of a file.
*/
export const getFileVersion = (key: string): TEndpointDec => {
export const getFileVersion = (key: string): TEndpointDecTuple => {
return [`/v1/files/${key}/versions`, []];
};
Loading

0 comments on commit 01691d6

Please sign in to comment.