Skip to content
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

All the TypeScript, Better Docs, More Flexibility, and Fewer Vulnerabilities #7

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
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
110 changes: 0 additions & 110 deletions index.js

This file was deleted.

10 changes: 4 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "telegra.ph",
"version": "1.0.1",
"description": "Tiny API helper for Telegra.ph",
"main": "index.js",
"main": "dist/index.js",
"scripts": {
"test": "eslint . && ava test"
},
Expand All @@ -25,8 +25,9 @@
},
"homepage": "https://github.com/telegraf/telegra.ph#readme",
"files": [
"index.js",
"typings/*.ts"
"dist",
"LICENSE",
"readme.md"
],
"devDependencies": {
"ava": "^1.2.0",
Expand All @@ -37,8 +38,5 @@
"eslint-plugin-node": "^8.0.1",
"eslint-plugin-promise": "^4.0.1",
"eslint-plugin-standard": "^4.0.0"
},
"dependencies": {
"isomorphic-fetch": "^2.2.1"
}
}
43 changes: 34 additions & 9 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# 📝 telegra.ph

[![NPM Version](https://img.shields.io/npm/v/telegra.ph.svg?style=flat-square)](https://www.npmjs.com/package/telegra.ph)
[![Build Status](https://img.shields.io/travis/telegraf/telegra.ph.svg?branch=master&style=flat-square)](https://travis-ci.org/telegraf/telegra.ph)

Expand All @@ -10,55 +11,79 @@ Tiny API helper for Telegra.ph.

## Installation

```js
```bash
$ npm install telegra.ph --save
```

## Example

### Create an account

```js
const Telegraph = require('telegra.ph')
import Telegraph from "telegra.ph"

const client = new Telegraph()

client.createAccount().then((account) => {
client.token = account.access_token
return client.getPageList()
}).then((pages) => console.log(pages))
const account = client.createAccount()
client.token = account.access_token

const pages = await client.getPageList()
console.log(pages)
```

### Use existing account

```js
const Telegraph = require('telegra.ph')
import Telegraph from "telegra.ph"

const client = new Telegraph(process.env.TOKEN)
client.getPageList().then((pages) => console.log(pages))

const pages = await client.getPageList()
console.log(pages)
```

## API documentation
### Use arbitrary HTTP Client

```js
import Telegraph from "telegra.ph"
import { Fetch } from "telegra.ph/clients"

const client = new Telegraph(process.env.TOKEN, { client: Fetch })

const pages = await client.getPageList()
console.log(pages)
```

## API documentation

#### [`createAccount`](http://telegra.ph/api#createAccount)

`.createAccount(shortName, name, url)`

#### [`createPage`](http://telegra.ph/api#createPage)

`.createPage(title, content, authorName, authorUrl, returnContent)`

#### [`editAccountInfo`](http://telegra.ph/api#editAccountInfo)

`.editAccountInfo(shortName, name, url)`

#### [`editPage`](http://telegra.ph/api#editPage)

`.editPage(path, title, content, authorName, authorUrl, returnContent)`

#### [`getPage`](http://telegra.ph/api#getPage)

`.getPage(path, returnContent)`

#### [`getViews`](http://telegra.ph/api#getViews)

`.getViews(path, year, month, day, hour)`

#### [`getPageList`](http://telegra.ph/api#getPageList)

`.getPageList(path, offset, limit)`

#### [`revokeAccessToken`](http://telegra.ph/api#revokeAccessToken)

`.revokeAccessToken()`
33 changes: 33 additions & 0 deletions src/clients/fetch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Client } from "./index"

export class Fetch implements Client {
constructor(public baseURL: string) {}

async get<R = unknown>(path: string): Promise<R> {
const response = await fetch(`${this.baseURL}${path}`)
const json = await response.json()
if (json.ok === false) {
if (json.error) throw new Error(json.error)
else throw new Error(json)
}
if (json.result) return json.result
else return json
}

async post<R = unknown>(path: string, body: unknown): Promise<R> {
const response = await fetch(`${this.baseURL}${path}`, {
method: "POST",
body: JSON.stringify(body),
headers: {
"Content-Type": "application/json",
},
})
const json = await response.json()
if (json.ok === false) {
if (json.error) throw new Error(json.error)
else throw new Error(json)
}
if (json.result) return json.result
else return json
}
}
50 changes: 50 additions & 0 deletions src/clients/https.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import type { Client } from "./"
import https from "https"

export class HTTPS implements Client {
constructor(public baseURL: string) {}

protected request<R = unknown>(method: string, path: string, data: unknown): Promise<R> {
return new Promise((resolve, reject) => {
const req = https.request(
`${this.baseURL}${path}`,
{
method,
headers: {
"Content-Type": "application/json",
},
},
(res) => {
let body = ""
res.on("data", (chunk) => {
body += chunk
})
res.on("end", () => {
try {
const obj = JSON.parse(body)
if (obj.ok === false) {
if (obj.error) reject(new Error(obj.error))
else reject(new Error(obj))
}
if (obj.result) resolve(obj.result)
else resolve(obj)
} catch (err) {
reject(err)
}
})
}
)
req.on("error", reject)
req.write(JSON.stringify(data))
req.end()
})
}

post<R = unknown>(path: string, data: unknown): Promise<R> {
return this.request("POST", path, data)
}

get<R = unknown>(path: string, data: unknown): Promise<R> {
return this.request("GET", path, data)
}
}
7 changes: 7 additions & 0 deletions src/clients/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export interface Client {
post: <R>(path: string, data: unknown) => Promise<R>
get: <R>(path: string, data: unknown) => Promise<R>
}

export { Fetch } from "./fetch"
export { HTTPS } from "./https"
Loading