Skip to content

Commit 27699b6

Browse files
authored
Merge pull request #67 from snek-at/update-client-aa1
Update client aa1 to implement 2
2 parents f5aa706 + a7814ec commit 27699b6

File tree

30 files changed

+635
-982
lines changed

30 files changed

+635
-982
lines changed

package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"main": "lib/index.js",
66
"types": "lib/index.d.ts",
77
"scripts": {
8-
"test": "echo \"Error: no test specified\" && exit 1",
8+
"test": "mocha --require ts-node/register src/test/**/*.ts",
99
"build": "npx tsc"
1010
},
1111
"files": [
@@ -24,17 +24,17 @@
2424
},
2525
"homepage": "https://github.com/snek-at/client#readme",
2626
"dependencies": {
27-
"apollo-cache-inmemory": "^1.6.5",
28-
"apollo-client": "^2.6.8",
29-
"apollo-link-http": "^1.5.16",
30-
"graphql": "^14.6.0",
27+
"@apollo/client": "^3.1.5",
28+
"apollo-upload-client": "^14.1.1",
29+
"graphql": "^14.7.0",
3130
"graphql-tag": "^2.10.3",
3231
"js-cookie": "^2.2.1"
3332
},
3433
"devDependencies": {
3534
"@babel/core": "^7.9.6",
3635
"@babel/plugin-proposal-class-properties": "^7.8.3",
36+
"@types/apollo-upload-client": "^14.1.0",
3737
"@types/js-cookie": "^2.2.6",
38-
"typescript": "^3.8.3"
38+
"typescript": "^3.9.7"
3939
}
4040
}

src/endpoints/apollo.ts

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
11
//#region > Imports
2-
//#PACKAGE "apollo-client"
3-
//## npm install "apollo-client"@2.6.8
4-
// Contains the client for graphql handling
5-
import { ApolloClient } from "apollo-client";
6-
//#PACKAGE "apollo-link-http"
7-
//## npm install "apollo-link-http"@1.5.16
8-
// Contains the link for the apollo client
9-
import { HttpLink } from "apollo-link-http";
10-
//#PACKAGE "apollo-cache-inmemory"
11-
//## npm install "apollo-cache-inmemory"@1.6.5
12-
// Contains cache handling for apollo
2+
//#PACKAGE "@apollo/client"
133
import {
4+
ApolloClient,
5+
ApolloLink,
146
InMemoryCache,
15-
IntrospectionFragmentMatcher,
167
NormalizedCacheObject,
17-
} from "apollo-cache-inmemory";
8+
HttpLink,
9+
} from "@apollo/client";
10+
//#PACKAGE "'apollo-upload-client"
11+
// Contains the link for the apollo client
12+
import { createUploadLink } from "apollo-upload-client";
1813
//#PACKAGE "graphql"
1914
//## npm install "graphql"@14.6.0
2015
// Contains the interface for gql queries, mutations and subscriptions
@@ -32,7 +27,7 @@ import { ApolloResult } from "./index";
3227
/** @class Apollo client for graphql handling */
3328
class Apollo implements ApolloEndpoint {
3429
//> Fields
35-
private link: HttpLink;
30+
private link: ApolloLink;
3631
private cache: InMemoryCache;
3732
private client: ApolloClient<NormalizedCacheObject>;
3833

@@ -49,26 +44,18 @@ class Apollo implements ApolloEndpoint {
4944
*/
5045
constructor(uri: string, options: Options) {
5146
this.headers = options.headers;
52-
const fragmentMatcher = new IntrospectionFragmentMatcher({
53-
introspectionQueryResultData: {
54-
__schema: {
55-
types: [],
56-
},
57-
},
58-
});
5947

6048
try {
61-
this.cache = new InMemoryCache({ fragmentMatcher });
49+
this.cache = new InMemoryCache();
6250
} catch {
6351
//#ERROR
6452
throw new Error("An error occurred while initializing the cache!");
6553
}
6654

6755
try {
68-
this.link = new HttpLink({
69-
uri,
70-
headers: options.headers,
71-
});
56+
const uploadLink = createUploadLink({ uri, headers: options.headers });
57+
58+
this.link = ApolloLink.from([uploadLink]);
7259
} catch {
7360
//#ERROR
7461
throw new Error("An error occurred while initializing the API link!");

src/endpoints/index.ts

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
//## npm install "graphql"@14.6.0
55
// Contains the interface for gql queries, mutations and subscriptions
66
import { DocumentNode } from "graphql";
7-
import { FetchResult } from "apollo-link";
8-
import { ApolloQueryResult } from "apollo-client";
7+
import { ApolloQueryResult, FetchResult } from "@apollo/client";
98
//#endregion
109

1110
//#region > Types
@@ -75,21 +74,21 @@ interface ScraperEndpoint extends Endpoint {
7574
/**
7675
* GetJson: A method which gets json data from a specific url.
7776
*
78-
* @param url A web url
77+
* @param path Path to the endpoint
7978
* @returns {Promise<T>} Json data in the given format <T>
8079
*/
8180
getJson<T>(url: string): Promise<T>;
8281
/**
8382
* GetDom: A method which gets DOM data from a specific url.
8483
*
85-
* @param url A web url
84+
* @param path Path to the endpoint
8685
* @returns {Promise<Document>} A DOM Document
8786
*/
8887
getDom(url: string): Promise<Document>;
8988
/**
9089
* Post: A method to post data to a specific url.
9190
*
92-
* @param {string} url A web url
91+
* @param {string} path Path to the endpoint
9392
* @param data Data which is filled into the body of a post request
9493
* @returns {Promise<Document>} A DOM Document
9594
*/
@@ -106,6 +105,52 @@ interface ScraperEndpoint extends Endpoint {
106105
| null
107106
| undefined
108107
): Promise<T>;
108+
/**
109+
* Send fetch request to a endpoint and get the respective result.
110+
*
111+
* @param {string} path Path to the endpoint. Specify it like "/foo/bar".
112+
* The correct placement of the slashes is essential!
113+
* @param {"GET" | "POST" | "PUT" | "PATCH" | "DELETE"} type HTTP methods
114+
* @param data Data which is filled into the body of a post request
115+
* @returns {Promise<Response>} A DOM Document
116+
*/
117+
fetch(
118+
path: string,
119+
type: "GET" | "POST" | "PUT" | "PATCH" | "DELETE",
120+
data?:
121+
| string
122+
| Blob
123+
| ArrayBufferView
124+
| ArrayBuffer
125+
| FormData
126+
| URLSearchParams
127+
| ReadableStream<Uint8Array>
128+
| null
129+
| undefined
130+
): Promise<Response>;
131+
/**
132+
* Send fetch request to a endpoint and get the respective JSON result.
133+
*
134+
* @param {string} path Path to the endpoint. Specify it like "/foo/bar".
135+
* The correct placement of the slashes is essential!
136+
* @param {"GET" | "POST" | "PUT" | "PATCH" | "DELETE"} type HTTP methods
137+
* @param data Data which is filled into the body of a post request
138+
* @returns {Promise<Response>} A DOM Document
139+
*/
140+
fetchJson<T>(
141+
path: string,
142+
type: "GET" | "POST" | "PUT" | "PATCH" | "DELETE",
143+
data?:
144+
| string
145+
| Blob
146+
| ArrayBufferView
147+
| ArrayBuffer
148+
| FormData
149+
| URLSearchParams
150+
| ReadableStream<Uint8Array>
151+
| null
152+
| undefined
153+
): Promise<T>;
109154
}
110155
//#endregion
111156

src/endpoints/scraper.ts

Lines changed: 75 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -69,22 +69,9 @@ class Scraper implements ScraperEndpoint {
6969
* @returns {object} DOM object
7070
*/
7171
async getDom(path: string): Promise<Document> {
72-
return fetch(this.url + path, {
73-
headers: {
74-
...this.headers,
75-
},
76-
})
77-
.then((response) => {
78-
if (!response.ok) {
79-
//#ERROR
80-
throw new Error(response.statusText);
81-
}
82-
83-
return response.text();
84-
})
85-
.then((text) => {
86-
return new DOMParser().parseFromString(text, "text/html");
87-
});
72+
const text = await (await this.fetch(path, "GET")).text();
73+
74+
return new DOMParser().parseFromString(text, "text/html");
8875
}
8976

9077
/**
@@ -123,6 +110,78 @@ class Scraper implements ScraperEndpoint {
123110
return response.json().then((data) => data as T);
124111
});
125112
}
113+
114+
/**
115+
* Send fetch request to a endpoint and get the respective result.
116+
*
117+
* @param {string} path Path to the endpoint. Specify it like "/foo/bar".
118+
* The correct placement of the slashes is essential!
119+
* @param {"GET" | "POST" | "PUT" | "PATCH" | "DELETE"} type HTTP methods
120+
* @param data Data which is filled into the body of a post request
121+
* @returns {Promise<Response>} A DOM Document
122+
*/
123+
async fetch(
124+
path: string,
125+
type: "GET" | "POST" | "PUT" | "PATCH" | "DELETE",
126+
data?:
127+
| string
128+
| Blob
129+
| ArrayBufferView
130+
| ArrayBuffer
131+
| FormData
132+
| URLSearchParams
133+
| ReadableStream<Uint8Array>
134+
| null
135+
| undefined
136+
): Promise<Response> {
137+
return fetch(this.url + path, {
138+
method: type,
139+
body: data,
140+
headers: {
141+
...this.headers,
142+
},
143+
}).then(async (response) => {
144+
if (!response.ok) {
145+
//#ERROR
146+
throw new Error(response.statusText);
147+
}
148+
149+
return response;
150+
});
151+
}
152+
153+
/**
154+
* Send fetch request to a endpoint and get the respective JSON result.
155+
*
156+
* @param {string} path Path to the endpoint. Specify it like "/foo/bar".
157+
* The correct placement of the slashes is essential!
158+
* @param {"GET" | "POST" | "PUT" | "PATCH" | "DELETE"} type HTTP methods
159+
* @param data Data which is filled into the body of a post request
160+
* @returns {Promise<Response>} A DOM Document
161+
*/
162+
async fetchJson<T>(
163+
path: string,
164+
type: "GET" | "POST" | "PUT" | "PATCH" | "DELETE",
165+
data?:
166+
| string
167+
| Blob
168+
| ArrayBufferView
169+
| ArrayBuffer
170+
| FormData
171+
| URLSearchParams
172+
| ReadableStream<Uint8Array>
173+
| null
174+
| undefined
175+
): Promise<T> {
176+
return this.fetch(path, type, data).then(async (response) => {
177+
if (!response.ok) {
178+
//#ERROR
179+
throw new Error(response.statusText);
180+
}
181+
182+
return response.json().then((data) => data as T);
183+
});
184+
}
126185
}
127186
//#endregion
128187

src/index.ts

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,16 @@
44
import Apollo from "./endpoints/apollo";
55
// Contains the scraper endpoint
66
import Scraper from "./endpoints/scraper";
7-
//> Templates
8-
// Contains the main template
9-
import { MainTemplate } from "./templates/index";
107
//> Sessions
118
// Contains the SNEK and github session
12-
import { SnekSession, GithubSession } from "./session/sessions";
9+
import {
10+
SnekSession,
11+
GithubSession,
12+
InstagramSession,
13+
} from "./session/sessions";
1314
//> Interfaces
1415
// Contains interfaces for scraper and apollo
1516
import { ScraperEndpoint, ApolloEndpoint } from "./endpoints/index";
16-
// Contains the interface for the main template
17-
import { IMainTemplate } from "./templates/index";
1817
//#endregion
1918

2019
//#region > Interfaces
@@ -64,7 +63,6 @@ class Client implements IClient {
6463
/** @class A client implementation for SNEK interaction */
6564
class SnekClient extends Client {
6665
gql: ApolloEndpoint;
67-
template: IMainTemplate;
6866
session: SnekSession;
6967

7068
/**
@@ -84,16 +82,14 @@ class SnekClient extends Client {
8482
) {
8583
super({ type, url, headers });
8684

87-
this.template = new MainTemplate();
8885
this.gql = new Apollo(url, { headers });
89-
this.session = new SnekSession("snek", this.gql, this.template.snek);
86+
this.session = new SnekSession("snek", this.gql);
9087
}
9188
}
9289

9390
/** @class A client implementation for github interaction */
9491
class GithubClient extends Client {
9592
gql: ApolloEndpoint;
96-
template: IMainTemplate;
9793
session: GithubSession;
9894

9995
/**
@@ -113,9 +109,39 @@ class GithubClient extends Client {
113109
) {
114110
super({ type, url, headers });
115111

116-
this.template = new MainTemplate();
117112
this.gql = new Apollo(url, { headers });
118-
this.session = new GithubSession("github", this.gql, this.template);
113+
this.session = new GithubSession("github", this.gql);
114+
}
115+
}
116+
117+
/** @class A client implementation for instagram interaction */
118+
class InstagramClient extends Client {
119+
ep: ScraperEndpoint;
120+
session: InstagramSession;
121+
122+
/**
123+
* Initializes a Github client.
124+
*
125+
* @constructor
126+
* @author Nico Schett <contact@schett.net>
127+
* @param url The base URL the InstagramClient should be working on.
128+
* Default: "https://graph.instagram.com"
129+
* @param headers A object containing various request headers
130+
* @param type A type description to differ between multiple instances
131+
*/
132+
constructor(
133+
accessToken: string,
134+
url: string = "https://graph.instagram.com",
135+
headers: {} = {},
136+
type: string = "scraper"
137+
) {
138+
super({ type, url, headers });
139+
140+
this.ep = new Scraper(url, {
141+
headers: { Authorization: `Bearer ${accessToken}` },
142+
});
143+
144+
this.session = new InstagramSession("instagram", accessToken, this.ep);
119145
}
120146
}
121147

@@ -148,7 +174,7 @@ class WebClient extends Client {
148174
//#endregion
149175

150176
//#region > Exports
151-
export { SnekClient, GithubClient, WebClient };
177+
export { SnekClient, GithubClient, InstagramClient, WebClient };
152178
//#endregion
153179

154180
/**

0 commit comments

Comments
 (0)