Skip to content

Commit 97ce33d

Browse files
authored
feat: Add Document trait in collection related structs. (#18)
Add CollectionClient Use workspaces Co-authored-by: Luis Moreno <[email protected]>
1 parent 3dd879a commit 97ce33d

36 files changed

+1235
-41
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,4 @@ jobs:
5656
- name: Tests in WASM
5757
if: matrix.platform.target == 'wasm32-unknown-unknown'
5858
run: wasm-pack test --headless --chrome
59+
working-directory: ./typesense

Cargo.toml

Lines changed: 5 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,5 @@
1-
[package]
2-
name = "typesense"
3-
version = "0.1.0"
4-
authors = ["Typesense <[email protected]>"]
5-
edition = "2018"
6-
7-
[features]
8-
default = []
9-
tokio-rt = ["hyper/runtime", "hyper/tcp", "hyper-tls"]
10-
11-
[lib]
12-
crate-type = ["cdylib", "rlib"]
13-
14-
[dependencies]
15-
async-trait = "0.1.50"
16-
base64 = "0.13.0"
17-
hmac = "0.11.0"
18-
http = "0.2.4"
19-
serde = { version = "1", features = ["derive"] }
20-
serde_json = "1"
21-
sha2 = "0.9.5"
22-
thiserror = "1.0.24"
23-
24-
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
25-
hyper = { version = "0.14.7", features = ["http1", "http2", "client"] }
26-
hyper-tls = { version = "0.5.0", optional = true }
27-
28-
[target.'cfg(target_arch = "wasm32")'.dependencies]
29-
js-sys = { version = "0.3.50" }
30-
wasm-bindgen = { version = "0.2.73" }
31-
wasm-bindgen-futures = { version = "0.4.23" }
32-
web-sys = { version = "0.3.50", features = ["Headers", "Response", "Request", "RequestInit", "RequestMode", "Window", "WorkerGlobalScope"] }
33-
34-
[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies]
35-
tokio = { version = "1.5.0", features = ["macros", "rt", "rt-multi-thread"] }
36-
37-
[target.'cfg(target_arch = "wasm32")'.dev-dependencies]
38-
console_error_panic_hook = "0.1.6"
39-
wasm-bindgen-test = "0.3.23"
1+
[workspace]
2+
members = [
3+
"typesense",
4+
"typesense_derive"
5+
]

mocks/create_collection.yaml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
when:
2+
method: POST
3+
path: /collections
4+
header:
5+
- name: X-TYPESENSE-API-KEY
6+
value: VerySecretKey
7+
body: '{"name":"companies","fields":[{"name":"company_name","type":"string"},{"name":"num_employees","type":"int32"},{"name":"country","type":"string","facet":true}],"default_sorting_field":"num_employees"}'
8+
then:
9+
status: 200
10+
header:
11+
- name: content-type
12+
value: text/json
13+
body: '
14+
{
15+
"name": "companies",
16+
"num_documents": 0,
17+
"fields": [
18+
{"name": "company_name", "type": "string" },
19+
{"name": "num_employees", "type": "int32" },
20+
{"name": "country", "type": "string", "facet": true }
21+
],
22+
"default_sorting_field": "num_employees"
23+
}'

mocks/drop_collection.yaml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
when:
2+
method: DELETE
3+
path: /collections/companies
4+
header:
5+
- name: X-TYPESENSE-API-KEY
6+
value: VerySecretKey
7+
status: 200
8+
then:
9+
header:
10+
- name: content-type
11+
value: text/json
12+
body: '
13+
{
14+
"name": "companies",
15+
"num_documents": 1200,
16+
"fields": [
17+
{"name": "company_name", "type": "string" },
18+
{"name": "num_employees", "type": "int32" },
19+
{"name": "country", "type": "string", "facet": true }
20+
],
21+
"default_sorting_field": "num_employees"
22+
}'

mocks/retrieve_all_collections.yaml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
when:
2+
method: GET
3+
path: /collections
4+
header:
5+
- name: X-TYPESENSE-API-KEY
6+
value: VerySecretKey
7+
status: 200
8+
then:
9+
header:
10+
- name: content-type
11+
value: text/json
12+
body: '
13+
[{
14+
"num_documents": 1250,
15+
"name": "companies",
16+
"fields": [
17+
{"name": "company_name", "type": "string"},
18+
{"name": "num_employees", "type": "int32"},
19+
{"name": "country", "type": "string", "facet": true}
20+
],
21+
"default_sorting_field": "num_employees"
22+
},
23+
{
24+
"num_documents": 1250,
25+
"name": "ceos",
26+
"fields": [
27+
{"name": "company_name", "type": "string"},
28+
{"name": "full_name", "type": "string"},
29+
{"name": "from_year", "type": "int32"}
30+
],
31+
"default_sorting_field": "num_employees"
32+
}]'

mocks/retrieve_collection.yaml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
when:
2+
method: GET
3+
path: /collections/companies
4+
header:
5+
- name: X-TYPESENSE-API-KEY
6+
value: VerySecretKey
7+
status: 200
8+
then:
9+
header:
10+
- name: content-type
11+
value: text/json
12+
body: '
13+
{
14+
"name": "companies",
15+
"num_documents": 1250,
16+
"fields": [
17+
{"name": "company_name", "type": "string" },
18+
{"name": "num_employees", "type": "int32" },
19+
{"name": "country", "type": "string", "facet": true }
20+
],
21+
"default_sorting_field": "num_employees"
22+
}'

typesense/Cargo.toml

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
[package]
2+
name = "typesense"
3+
version = "0.1.0"
4+
authors = ["Typesense <[email protected]>"]
5+
edition = "2018"
6+
7+
[features]
8+
default = ["derive"]
9+
tokio-rt = ["hyper/runtime", "hyper/tcp", "hyper-tls"]
10+
11+
# Provide derive(Document) macro.
12+
derive = ["typesense_derive"]
13+
14+
[lib]
15+
crate-type = ["cdylib", "rlib"]
16+
17+
[dependencies]
18+
async-trait = "0.1.50"
19+
base64 = "0.13.0"
20+
hmac = "0.11.0"
21+
http = "0.2.4"
22+
serde = { version = "1", features = ["derive"] }
23+
serde_json = "1"
24+
sha2 = "0.9.5"
25+
thiserror = "1.0.24"
26+
typesense_derive = { path="../typesense_derive", optional = true }
27+
28+
[dev-dependencies]
29+
serde_json = "1.0"
30+
trybuild = "1.0.42"
31+
32+
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
33+
hyper = { version = "0.14.7", features = ["http1", "http2", "client"] }
34+
hyper-tls = { version = "0.5.0", optional = true }
35+
36+
[target.'cfg(target_arch = "wasm32")'.dependencies]
37+
js-sys = { version = "0.3.50" }
38+
wasm-bindgen = { version = "0.2.73" }
39+
wasm-bindgen-futures = { version = "0.4.23" }
40+
web-sys = { version = "0.3.50", features = ["Headers", "Response", "Request", "RequestInit", "RequestMode", "Window", "WorkerGlobalScope"] }
41+
42+
[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies]
43+
tokio = { version = "1.5.0", features = ["macros", "rt", "rt-multi-thread"] }
44+
45+
[target.'cfg(target_arch = "wasm32")'.dev-dependencies]
46+
console_error_panic_hook = "0.1.6"
47+
wasm-bindgen-test = "0.3.23"
48+
49+
[[test]]
50+
name = "derive_tests"
51+
path = "tests/derive/lib.rs"
52+
required-features = ["derive"]
File renamed without changes.
File renamed without changes.

src/client/mod.rs renamed to typesense/src/client/mod.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::sync::Arc;
22

33
use http::Response;
44

5+
use crate::collection::CollectionClient;
56
use crate::transport::HttpLowLevel;
67
use crate::transport::Transport;
78
use crate::Result;
@@ -12,7 +13,6 @@ pub mod keys;
1213
pub use builder::ClientBuilder;
1314
pub use keys::ClientKeys;
1415

15-
#[allow(dead_code)]
1616
pub const TYPESENSE_API_KEY_HEADER_NAME: &str = "X-TYPESENSE-API-KEY";
1717

1818
/// Root client for top level APIs
@@ -40,6 +40,12 @@ where
4040
client: self.clone(),
4141
}
4242
}
43+
/// Creates a [`CollectionClient`] to interact with the Typesense Collection API
44+
pub fn collection(&self) -> CollectionClient<T> {
45+
CollectionClient {
46+
client: self.clone(),
47+
}
48+
}
4349
}
4450

4551
#[allow(dead_code)]

0 commit comments

Comments
 (0)