{"id":18572398,"url":"https://github.com/a/client-rest-framework","last_synced_at":"2025-04-10T07:31:19.251Z","repository":{"id":80284767,"uuid":"605965430","full_name":"A/client-rest-framework","owner":"A","description":"REST API framework to construct domain API repositories","archived":false,"fork":false,"pushed_at":"2024-10-03T13:08:38.000Z","size":310,"stargazers_count":4,"open_issues_count":2,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-21T17:55:24.494Z","etag":null,"topics":["django-rest-framework","frontend"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/A.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":".github/CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2023-02-24T09:41:24.000Z","updated_at":"2025-01-11T17:45:18.000Z","dependencies_parsed_at":null,"dependency_job_id":"b2b72563-8ec5-49f0-b56b-dc29c3733ce0","html_url":"https://github.com/A/client-rest-framework","commit_stats":{"total_commits":25,"total_committers":1,"mean_commits":25.0,"dds":0.0,"last_synced_commit":"065f09806965a54929b47d1b0feb5d5a52e4b89e"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/A%2Fclient-rest-framework","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/A%2Fclient-rest-framework/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/A%2Fclient-rest-framework/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/A%2Fclient-rest-framework/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/A","download_url":"https://codeload.github.com/A/client-rest-framework/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248176306,"owners_count":21060044,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["django-rest-framework","frontend"],"created_at":"2024-11-06T23:06:05.495Z","updated_at":"2025-04-10T07:31:18.968Z","avatar_url":"https://github.com/A.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# client-rest-framework\n\n[![Build and Test](https://github.com/A/client-rest-framework/actions/workflows/ci.yml/badge.svg)](https://github.com/A/client-rest-framework/actions/workflows/ci.yml)\n[![npm version](https://badge.fury.io/js/client-rest-framework.svg)](https://badge.fury.io/js/client-rest-framework)\n\nWORK IN PROGRESS.\n\n# Introduction\n\nThe `client-rest-framework` is a Typescript library that allows developers to create repositories to manage entities in a few lines of code, in a style similar to Django REST framework. The library provides APIs, serializers, and repositories that make it easy to interact with RESTful APIs and manage entities in a structured and efficient manner.\n\n\n## Features\n\n- Powerful and flexible two-directional serializers for creating your own serialization and deserialization logic.\n- Support for custom serializers to tailor serialization to your specific needs.\n- Strongly-typed entities based on the defined serializers, providing type safety and avoiding common errors.\n- Built-in support for Axios API, with the ability to easily customize and use custom API classes.\n- Object-oriented design inspired by Django REST framework, making it easy to manage entities in a structured and efficient manner.\n\n# Installation\n\nTo install the `client-rest-framework`, run the following command:\n\n`npm install client-rest-framework`\n\n# Usage\n\nTo use the library, you need to import the `repositories` and `serializers` modules from the `client-rest-framework` package, and define your API endpoints, serializers, and repositories.\n\n## API\n\nTo define an API, create a class that extends the `RESTAPI` class, provide an `HTTPClient` instance, and set the `url` property to the API endpoint URL. You can then pass this API to an `ApiRepository` as a realization of CRUD operations for the corresponding entity.\n\nExample API class:\n\n```typescript\nimport { api, pagination } from \"client-rest-framework\";\nimport { PublicUserDTO } from \"./types\"; \n\n// Configure HTTPClient\nclass HTTPClient extends api.AxiosHTTPClient {\n  getExtraHeaders() {\n    return { Authorization: `Bearer ${access}` };\n  }\n\n  onUnauthenticate = () =\u003e {\n    return;\n  }\n}\n\nclass API\u003cT\u003e extends api.RESTAPI\u003cT\u003e {\n  client = new HTTPClient({\n    baseURL: BASE_URL\n  });\n\n}\n\nclass PublicUserAPI extends API\u003cPublicUserDTO\u003e {\n  pagination = pagination.PageNumberPagination\u003cPublicUserDTO\u003e()\n\turl = \"/api/users\";\n}\n```\n\n## Serializers\n\nSerializers are two-directional data-mappers that help to explicitly describe the domain model and to enforce its types in a simple case.\n\nTo define a serializer, create a class that extends the `ModelSerializer` class, and define fields for each attribute of the corresponding entity. You can use the `StringField`, `NumberField`, `BooleanField`, `DateField`, and `EnumField` classes to define different types of fields.\n\nExample serializer class:\n\n```typescript\nimport { serializers } from \"client-rest-framework\"; \nimport { PublicUserDTO, RoleKey, UserStatusKey, CategoryKey } from \"./types\";  \n\nexport class PublicUserSerializer extends ModelSerializer\u003cPublicUserDTO\u003e {\n\tid = new serializers.NumberField({ readonly: true });\n\tusername = new serializers.StringField({ readonly: true });\n\temail = new serializers.StringField({ readonly: true });\n\tdisplay_name = new serializers.StringField({ readonly: true });\n\tdate_joined = new serializers.DateField({ readonly: true });\n\tnotes = new serializers.StringField({});\n\tphone = new serializers.StringField({});\n\t// TS limutation, in this case you need to pass T, Readonly and Many generics explicitly:\n\troles = new serializers.EnumField\u003cRoleKey, false, true\u003e({ many: true });\n\tstatus = new serializers.EnumField\u003cUserStatusKey, false, false\u003e({});\n\tcategories = new serializers.EnumField\u003cCategoryKey, false, true\u003e({ many: true });\n}\n```\n\nThe library supports inferring the resulting domain type from the serializer class using the `ReturnType` type operator. You can use this to get the type of the entity returned by the repository's methods.\n\nExample:\n\n```typescript\nexport type PublicUser = ReturnType\u003cPublicUserSerializer[\"fromDTO\"]\u003e\n```\n\n## Repositories\n\nTo define a repository, create a class that extends the `APIRepository` class, and set the `api` property to an instance of the corresponding API class, and the `serializer` property to an instance of the corresponding serializer class.\n\nExample repository class:\n\n```typescript\nimport { repositories } from \"client-rest-framework\"; \nimport { PublicUserDTO } from \"./types\";\nimport { PublicUserAPI } from \"./api\";\nimport { PublicUserSerializer } from \"./serializers\";\n\nexport class PublicUserApiRepository extends repositories.APIRepository {\n\tapi = new PublicUserAPI();\n\tserializer = new PublicUserSerializer();\n}\n```\n\n## Using the repository\n\nYou can now use the repository to interact with the API and manage entities.\n\nExample usage:\n\n```typescript\nimport { PublicUserApiRepository } from \"./repositories\";\nimport { PublicUser } from \"./types\";\n\nconst publicUsersRepository = new PublicUserApiRepository();  \n\n\n// Get a user by ID \nconst user = await publicUsersRepository.get(1);\n\n// List all users\nconst users = await publicUsersRepository.list();\n\n// Create a new user\nconst user = await publicUsersRepository.create(data);\n\n// update a user\nconst user = await publicUsersRepository.update(1, diff);\n\n// delete a user\nawait repo.delete(1);\n```\n\n## Important Notes and Limitations\n\n- So far, list method works only with `rest_framework.pagination.PageNumberPagination` and expects this setting to be configured in DRF.\n- Package is in early alpha, interfaces may change\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fa%2Fclient-rest-framework","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fa%2Fclient-rest-framework","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fa%2Fclient-rest-framework/lists"}