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

Turn all the source files into TypeScript #12

Open
wants to merge 17 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
46 changes: 46 additions & 0 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions

name: Node.js CI

on:
push:
branches:
- main
- master
pull_request:
branches:
- main
- master
schedule:
- cron: '0 2 * * *'

jobs:
build:
runs-on: ${{ matrix.os }}

strategy:
fail-fast: false
matrix:
node-version: [8, 10]
os: [ubuntu-latest, windows-latest, macos-latest]

steps:
- name: Checkout Git Source
uses: actions/checkout@v2

- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}

- name: Install Dependencies
run: npm i -g npminstall && npminstall

- name: Continuous Integration
run: npm run ci

- name: Code Coverage
uses: codecov/codecov-action@v1
with:
token: ${{ secrets.CODECOV_TOKEN }}
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ run/
.DS_Store
*.swp
.vscode

yarn.lock
lib/
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
sudo: false
language: node_js
node_js:
- '8'
- '10'
before_install:
- npm i npminstall -g
install:
- npm i npminstall && npminstall
- npminstall
script:
- npm run ci
after_script:
Expand Down
11 changes: 8 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@yuque/sdk",
"version": "1.1.1",
"version": "1.2.0",
"description": "Node SDK for yuque",
"main": "lib/index.js",
"dependencies": {
Expand All @@ -9,11 +9,15 @@
"urllib": "^2.33.1"
},
"devDependencies": {
"@types/debug": "^4.1.7",
"@types/node": "^17.0.23",
"autod": "^3.0.1",
"egg-bin": "^4.3.7",
"egg-ci": "^1.8.0",
"eslint": "^4.18.1",
"eslint-config-egg": "^7.0.0"
"eslint-config-egg": "^7.0.0",
"ts-node": "^10.7.0",
"typescript": "^4.6.3"
},
"engines": {
"node": ">=8.0.0"
Expand All @@ -25,7 +29,8 @@
"test-local": "egg-bin test",
"cov": "egg-bin cov",
"ci": "npm run lint && egg-bin pkgfiles --check && npm run cov",
"pkgfiles": "egg-bin pkgfiles"
"pkgfiles": "egg-bin pkgfiles",
"build": "tsc -p ."
},
"ci": {
"version": "8, 10"
Expand Down
File renamed without changes.
65 changes: 65 additions & 0 deletions src/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import assert from "assert";
import httpclient, {RequestOptions} from 'urllib';

const extend = require('extend2');
import DBG from 'debug'
const debug=DBG('yuque');

import {YuqueOptions} from "./index";
class CError extends Error{
public status?:string;
public code?:number;
public data?:any;
public constructor(message?:string) {
super(message);
}
}

export default class Client {
private options: YuqueOptions;

public constructor(options: YuqueOptions) {
this.options = Object.assign({
endpoint: 'https://www.yuque.com/api/v2',
userAgent: '@yuque/sdk',
}, options);
assert(this.options.token, 'token is required');
}

public async request(api:string,opts:RequestOptions) {
const {endpoint, token, userAgent, requestOpts, handler} = this.options;
const url=`${endpoint}${api}`;
opts=extend(true,{
method: 'GET',
contentType: 'json',
dataType: 'json',
headers: {
'User-Agent': userAgent,
'X-Auth-Token': token,
},
gzip: true,

// proxy
rejectUnauthorized: !process.env.http_proxy,
enableProxy: !!process.env.http_proxy,
proxy: process.env.http_proxy,
}, requestOpts, opts);
debug(`${opts.method},${url}`);

const res=await httpclient.request(url,opts);
debug('response',res.data);

//custom response
if(handler) return handler(res);

//default handler
if(res.status !== 200){
const err=new CError(res.data.message);
/* istanbul ignore next */
err.status = res.data.status || res.status;
err.code = res.data.code;
err.data = res;
throw err;
}
}
}
File renamed without changes.
69 changes: 69 additions & 0 deletions src/doc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import assert from "assert";
import Client from "./client";
import {GArg} from "./index";

interface DocListArg{
namespace:string,
}
interface DocGetArg{
namespace:string,
slug:string,
data?:{
raw:number
}
}
type DocFormat='markdown'|'lake';
interface DocCreateArg{
namespace:string,
data:{
title:string,
slug:string,
public?:number,
format?:DocFormat,
body:string,
}
}
interface DocUpdateArg{
namespace:string,
id:string,
data:{
title?:string,
slug?:string,
public?:number,
body?:string
}
}
interface DocDeleteArg{
namespace:string,
id:string,
}

export default class Doc{
private client:Client;
constructor({client}:GArg) {
this.client=client;
}
public async list({namespace}:DocListArg){
assert(namespace,'repo namespace or id is required');
return this.client.request(`repos/${namespace}/docs`, { method: 'GET' });
}
public async get({namespace,slug,data}:DocGetArg){
assert(namespace,'repo namespace or id is required');
assert(slug,'doc slug or id is required');
return this.client.request(`repos/${namespace}/docs/${slug}`, { method: 'GET', data });
}
public async create({namespace,data}:DocCreateArg){
assert(namespace, 'repo namespace or id is required');
return this.client.request(`repos/${namespace}/docs`, { method: 'POST', data });
}
public async update({ namespace, id, data }:DocUpdateArg) {
assert(namespace, 'repo namespace or id is required');
assert(id, 'doc id is required');
return this.client.request(`repos/${namespace}/docs/${id}`, { method: 'PUT', data });
}
public async delete({ namespace, id }:DocDeleteArg) {
assert(namespace, 'repo namespace or id is required');
assert(id, 'doc id is required');
return this.client.request(`repos/${namespace}/docs/${id}`, { method: 'DELETE' });
}
}
File renamed without changes.
62 changes: 62 additions & 0 deletions src/group.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import assert from "assert";
import {GArg, QArg} from "./index";
import Client from "./client";

interface GCreate{
data:{
name:string,
login:string,
description?:string,
}
}
interface GGetArg {
login:string
}
interface GAddUserArg{
group:string,
user:string,
data:{
role:0|1,
}
}
interface GUArg{
group:string,
user:string,
}

export default class Group {
private client: Client;

public constructor({client}: GArg) {
this.client = client;
}

public async list({login}: QArg = {}) {
const api: string = login ? `users/${login}/groups` : 'groups';
return this.client.request(api, {method: 'GET'});
}
public async create({ data }:GCreate) {
assert(data, 'data is required');
assert(data.name, 'data.name is required');
assert(data.login, 'data.login is required');
return this.client.request('groups', { method: 'POST', data });
}
public async get({ login }:GGetArg) {
assert(login, 'group login or id is required');
return this.client.request(`groups/${login}`, { method: 'GET' });
}
public async listUser({ login }:GGetArg) {
assert(login, 'group login or id is required');
return this.client.request(`groups/${login}/users`, { method: 'GET' });
}
public async addUser({ group, user, data }:GAddUserArg) {
assert(group, 'group login or id is required');
assert(user, 'user login or id is required');
return this.client.request(`groups/${group}/users/${user}`, { method: 'PUT', data });
}
public async removeUser({ group, user }:GUArg) {
assert(group, 'group login or id is required');
assert(user, 'user login or id is required');
return this.client.request(`groups/${group}/users/${user}`, { method: 'DELETE' });
}
}
File renamed without changes.
38 changes: 38 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import Client from './client';
import Group from "./group";
import User from "./user";
import Repo from "./repo";
import Doc from "./doc";
import {RequestOptions} from 'urllib';

export interface YuqueOptions {
token: string,
endpoint?: string,
userAgent?: string,
requestOpts?:RequestOptions,
handler?:(res:any)=>any,
}
export interface GArg{
client:Client,
}
export interface QArg{
login?:string,
}

export default class Yuque {
private options:YuqueOptions;
public client:Client;
public users:User;
public groups:Group;
public repos:Repo;
public docs:Doc;
public constructor(options: YuqueOptions) {
this.options = options;
const client = new Client(this.options);
this.client=client;
this.users = new User({client});
this.groups = new Group({client});
this.repos = new Repo({client});
this.docs = new Doc({client});
}
}
File renamed without changes.
Loading