Skip to content

Commit

Permalink
bump: deno v0.9.0 (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
keroxp authored Jun 16, 2019
1 parent 7da47d2 commit 1e15a9c
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 34 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ before_install:
- docker run -d -p 127.0.0.1:5984:5984 -t couchdb:2.3.0

install:
- curl -fsSL https://deno.land/x/install/install.sh | sh -s -- v0.7.0
- curl -fsSL https://deno.land/x/install/install.sh | sh -s -- v0.9.0
- export PATH="$HOME/.deno/bin:$PATH"

script:
Expand Down
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# deno-couchdb

![https://travis-ci.com/keroxp/deno-couchdb](https://travis-ci.com/keroxp/deno-couchdb.svg?branch=master)
![https://img.shields.io/github/tag/keroxp/deno-couchdb.svg](https://img.shields.io/github/tag/keroxp/deno-couchdb.svg)
[![license](https://img.shields.io/github/license/keroxp/deno-couchdb.svg)](https://github.com/keroxp/deno-couchdb)
[![tag](https://img.shields.io/badge/deno__std-v0.7.0-green.svg)](https://github.com/denoland/deno_std)
[![tag](https://img.shields.io/badge/deno-v0.7.0-green.svg)](https://github.com/denoland/deno)
[![tag](https://img.shields.io/badge/deno__std-v0.9.0-green.svg)](https://github.com/denoland/deno_std)
[![tag](https://img.shields.io/badge/deno-v0.9.0-green.svg)](https://github.com/denoland/deno)

CouchDB client for Deno built top of fetch

Expand Down Expand Up @@ -43,6 +44,7 @@ async function main() {
await db.delete(id);
}
```

# Compatibility Table

## Document
Expand All @@ -63,3 +65,8 @@ async function main() {
## Server

- WIP...

## Contributing

WELCOME!
There are still missing features and actually I'm not familiar with CouchDB😇
55 changes: 25 additions & 30 deletions couch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import Reader = Deno.Reader;
import Buffer = Deno.Buffer;
import copy = Deno.copy;
import Response = domTypes.Response;
import BodySource = body.BodySource;
import RequestInit = domTypes.RequestInit;

export type CouchResponse = {
id: string;
Expand Down Expand Up @@ -93,7 +91,14 @@ export type CouchOptions = {
};
};

type FetchFunctionLike = (url: string, opts?: RequestInit) => Promise<Response>;
type FetchFunctionLike = (
url: string,
opts: {
method: string;
body?: string | ArrayBuffer;
headers?: Headers;
}
) => Promise<Response>;
function makeFetch(
endpoint: string,
opts: CouchOptions = {}
Expand All @@ -105,10 +110,10 @@ function makeFetch(
body,
headers = new Headers()
}: {
method?: string;
method: string;
body?: string | ArrayBuffer;
headers?: Headers;
body?: BodySource;
} = {}
}
) => {
if (opts.basicAuth) {
const { username, password } = opts.basicAuth;
Expand Down Expand Up @@ -197,7 +202,8 @@ class CouchDatabase<T> {
revs_info: boolean;
}>
): Promise<(CouchDocument & T) | NotModified> {
return this._get("json", id, opts);
const res = await this._get("json", id, opts);
return res.json();
}

async getMultipart(
Expand All @@ -216,12 +222,12 @@ class CouchDatabase<T> {
revs: boolean;
revs_info: boolean;
}>
): Promise<Response | NotModified> {
): Promise<Response> {
return this._get("multipart", id, opts);
}

private async _get<A extends "json" | "multipart">(
accept: A,
private async _get(
accept: "json" | "multipart",
id: string,
opts?: Partial<{
attachments: boolean;
Expand All @@ -237,12 +243,7 @@ class CouchDatabase<T> {
revs: boolean;
revs_info: boolean;
}>
): Promise<
{
json: (CouchDocument & T) | NotModified;
multipart: Response | NotModified;
}[A]
> {
): Promise<Response> {
const params = new URLSearchParams();
if (opts != null) {
if (opts.attachments != null) {
Expand All @@ -259,14 +260,8 @@ class CouchDatabase<T> {
method: "GET",
headers: new Headers({ accept })
});
if (res.status === 200) {
if (accept === "multipart") {
return res;
} else if (accept === "json") {
return res.json();
}
} else if (res.status === 304) {
return NotModified;
if (res.status === 200 || res.status === 304) {
return res;
}
throw new CouchError(res.status, await res.text());
}
Expand Down Expand Up @@ -370,9 +365,9 @@ class CouchDatabase<T> {
}
const res = await this.fetch(`/${id}?${params.toString()}`, {
method: "DELETE",
headers: {
headers: new Headers({
accept: "application/json"
}
})
});
if (res.status === 200 || res.status === 202) {
return res.json();
Expand All @@ -394,7 +389,7 @@ class CouchDatabase<T> {
const res = await this.fetch(`/${id}/${attachment}?${params.toString()}`, {
method: "GET",
headers: new Headers({
"accept": "application/json"
accept: "application/json"
})
});
if (res.status === 200) {
Expand Down Expand Up @@ -441,7 +436,7 @@ class CouchDatabase<T> {
const params = new URLSearchParams();
const headers = new Headers({
"content-type": contentType,
"accept": "application/json"
accept: "application/json"
});
if (rev != null) {
params.append("rev", rev);
Expand Down Expand Up @@ -536,7 +531,7 @@ class CouchDatabase<T> {
method: "POST",
headers: new Headers({
"content-type": "application/json",
"accept": "application/json"
accept: "application/json"
}),
body
});
Expand Down Expand Up @@ -577,7 +572,7 @@ export class CouchClient {
}

async getDatabase(name: string): Promise<CouchDatabaseInfo> {
const res = await this.fetch(`/${name}`);
const res = await this.fetch(`/${name}`, { method: "GET" });
if (res.status === 200) {
return res.json();
}
Expand Down
2 changes: 1 addition & 1 deletion couch_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
import open = Deno.open;

const kDbName = "testdb";
const env= Deno.env();
const env = Deno.env();
const endpoint = env["COUCHDB_ENDPOINT"] || "http://127.0.0.1:5984";
const client = new CouchClient(endpoint);
const db = client.database(kDbName);
Expand Down

0 comments on commit 1e15a9c

Please sign in to comment.