Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/badsyntax/lamernews-api-client
Lamer News API client
https://github.com/badsyntax/lamernews-api-client
api-client lamernews openapi swagger typescript
Last synced: 12 days ago
JSON representation
Lamer News API client
- Host: GitHub
- URL: https://github.com/badsyntax/lamernews-api-client
- Owner: badsyntax
- License: mit
- Created: 2021-05-20T20:41:30.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2021-05-23T09:58:35.000Z (over 3 years ago)
- Last Synced: 2024-10-11T20:11:41.543Z (about 1 month ago)
- Topics: api-client, lamernews, openapi, swagger, typescript
- Homepage:
- Size: 41 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE.md
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
# Lamer News API Client
[![npm version](https://badge.fury.io/js/lamernews-api-client.svg)](https://www.npmjs.com/package/lamernews-api-client)
A JavaScript API client for the Lamer News API. This package exports ES modules and includes TypeScript definitions.
**Note this package is still under development.**
Source code is generated from the [OpenAPI spec](./api/lamernews-schema.yml) using the [typescript-fetch](https://openapi-generator.tech/docs/generators/typescript-fetch/) template.
## Getting Started
### Install
```bash
npm install lamernews-api-client --save
```### Basic Usage
```ts
import { Configuration, NewsApi, SortType } from 'lamernews-api-client';const apiConfig = new Configuration({
basePath: 'https://echojs.com',
});const newsApi = new NewsApi(apiConfig);
newsApi
.getNews({
sort: SortType.Top,
start: 0,
count: 30,
})
.then((response) => {
console.log(response);
});
```### Usage with Middleware & TypeScript
```ts
import {
AuthApi,
CommentsApi,
Configuration,
ConfigurationParameters,
FetchParams,
GenericResponse,
Middleware,
NewsApi,
ResponseContext,
RequestContext,
ResponseStatus,
} from 'lamernews-api-client';class ApiMiddleWare implements Middleware {
public async pre(context: RequestContext): Promise {
return Promise.resolve(context);
}public async post(context: ResponseContext): Promise {
// Treat successful responses with response.status === 'err' as errors
if (
context.response.ok &&
context.response.headers
.get('content-type')
?.startsWith('application/json')
) {
const response = (await context.response
.clone()
.json()) as GenericResponse;
if (response.status === ResponseStatus.Err) {
return Promise.reject(context.response);
}
}
return Promise.resolve(context.response);
}
}const configParams: ConfigurationParameters = {
basePath: 'https://echojs.com',
middleware: [new ApiMiddleWare()],
};const apiConfig = new Configuration(configParams);
export const apiClient = {
newsApi: new NewsApi(apiConfig),
commentsApi: new CommentsApi(apiConfig),
authApi: new AuthApi(apiConfig),
};export type ApiClient = typeof apiClient;
```### Authentication
Here's an example of how to authenticate, then upvote a new entry:
```ts
authApi
.login({
username: 'username',
password: 'password',
})
.then((data) => {
const { auth, apisecret } = data;
document.cookie =
'auth=' + auth + '; expires=Thu, 1 Aug 2030 20:00:00 UTC; path=/';
return newsApi.voteNews({
newsId: newsId,
voteType: VoteType.Up,
apisecret: apisecret,
});
});
```### Authentication in React Native
Similar to the previous example we need to manually set the cookie. This can be achieved by updating the api config.
First you need to expose a function to mutate the config:
```ts
const configParams = {
basePath: 'https://echojs.com/api',
middleware: [new ApiMiddleWare()],
};const apiConfig = new Configuration(configParams);
export const apiClient = {
newsApi: new NewsApi(apiConfig),
commentsApi: new CommentsApi(apiConfig),
authApi: new AuthApi(apiConfig),
};export function updateApiConfig(config: ConfigurationParameters): void {
Object.assign(configParams, config);
}
```Now after a successful login, add the auth cookie header to the API config:
```ts
authApi
.login({
username: 'username',
password: 'password',
})
.then((data) => {
const { auth } = data;
updateApiConfig({
headers: {
cookie: `auth=${auth}`,
},
});
});
```All subsequent requests will now include this request header containing the cookie.
React-Native's `fetch` implementation does not support posting a `URLSearchParam` instance as the body. You need to adjust the request body to stringify the `URLSearchParams`:
```ts
export class ApiMiddleWare implements Middleware {
public async pre(context: RequestContext): Promise {
if (context.init.body instanceof URLSearchParams) {
context.init.body = context.init.body.toString();
}
return Promise.resolve(context);
}
}
```