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

🚧 Implement 2 to master (v0.2.0) #63

Closed
wants to merge 59 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
4aefc07
Add Config
schettn Aug 5, 2020
b0f1796
Exchange hard coded credentials with config credentials
schettn Aug 5, 2020
1a88e6b
Update SnekSession Begin Return Value
schettn Aug 5, 2020
84d6469
Merge pull request #60 from snek-at/improve-tasks-aa1
schettn Aug 5, 2020
78d234c
Update Registration Mutation
schettn Aug 5, 2020
4468f0f
Update GitlabServer Query
schettn Aug 5, 2020
6966921
Add AllUserPageUrls Query
schettn Aug 5, 2020
6a84565
Update Profile Query
schettn Aug 5, 2020
3ff6097
Update GitlabServerData Interface
schettn Aug 5, 2020
f11310c
Update AllPageUrlData Interface
schettn Aug 5, 2020
eb570ee
Improve Code Quality
schettn Aug 5, 2020
ce78e5a
Add AllUserPageUrls Task and Interface
schettn Aug 5, 2020
9b2351c
Fix Syntax Error in AllUserPageUrls
schettn Aug 5, 2020
2cfd5da
Update Default SnekClient URL
schettn Aug 5, 2020
b1b4eeb
Update TokenAuth Mutation
schettn Aug 5, 2020
c311e6c
Update Whoami Query
schettn Aug 5, 2020
6aec1dd
Update AllUserPageUrls Query
schettn Aug 5, 2020
11d43fc
Update Profile Task
schettn Aug 5, 2020
2dbf638
Update Default SnekClient URL
schettn Aug 5, 2020
250d5fb
Merge pull request #61 from snek-at/update-snekset-aa1
schettn Aug 5, 2020
628d8f6
Bump to v0.2.0
schettn Aug 5, 2020
eb2b7e7
Update registration mutation
schettn Aug 12, 2020
2a740a5
Update cache mutation
schettn Aug 12, 2020
b9402d6
Update gitlabserver query
schettn Aug 12, 2020
f126002
Update alluserpages query
schettn Aug 12, 2020
8a28068
Update profile query
schettn Aug 12, 2020
c73ba3a
Update profile task
schettn Aug 12, 2020
eacfd7f
Add type declaration
schettn Aug 12, 2020
733a509
Update token aliveness
schettn Aug 12, 2020
115b57e
Merge pull request #65 from snek-at/improve-errorhandling-aa1
schettn Aug 24, 2020
37b77da
Update profile query and task
schettn Aug 24, 2020
f077450
Improve profile task
schettn Aug 26, 2020
214ca59
Update cache mutation and task
schettn Aug 26, 2020
f5aa706
Merge pull request #66 from snek-at/update-snekset-ba1
schettn Sep 1, 2020
48ff645
Add instagram session
schettn Sep 4, 2020
a8a23d3
Rename instagram uri to path
schettn Sep 5, 2020
6b75033
Optimise tasks and clients
schettn Sep 5, 2020
018556c
Merge branch 'add-instagram-aa1' into update-client-aa1
schettn Sep 6, 2020
71a865b
Merge branch 'improve-templates-aa1' into update-client-aa1
schettn Sep 6, 2020
4de7dcc
Support more HTTP methods
schettn Sep 6, 2020
89eb6fe
Merge branch 'improve-scraper-aa2' into update-client-aa1
schettn Sep 6, 2020
9d800f2
Fix invalid up to date token
schettn Sep 6, 2020
c4ee278
Merge branch 'improve-templates-ba1' into update-client-aa1
schettn Sep 6, 2020
cee2d68
Improve snek session begin return type
schettn Sep 6, 2020
d81b62c
Improve instagram client defaults
schettn Sep 6, 2020
d45a405
Fix task run error loop
schettn Sep 9, 2020
21f7fd9
Add cookie options
schettn Sep 9, 2020
7edb688
Add support for uploading files
schettn Sep 10, 2020
8e3103d
Fix token refresh issue with invalid refresh token
schettn Sep 10, 2020
a03b509
Cleanup
schettn Nov 7, 2020
b5dfd3b
Update package.json
schettn Nov 7, 2020
18acb6c
Implement codacy suggestions
schettn Nov 7, 2020
b85fc7b
Apply suggestions from code review
schettn Nov 9, 2020
4521d9c
Improve regions and comments
schettn Nov 9, 2020
5659b23
Apply suggestions from code review
schettn Nov 10, 2020
a7814ec
Improve comments
schettn Nov 10, 2020
27699b6
Merge pull request #67 from snek-at/update-client-aa1
schettn Nov 11, 2020
2e5a67d
Hotfix: Change compiler to babel
schettn Nov 26, 2020
b7f7b86
Add support for NodeJS
schettn Nov 27, 2020
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
11 changes: 11 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"presets": [
["@babel/preset-env",{"targets": {"node": "current"}}],
"@babel/preset-typescript"
],
"plugins": [
"@babel/plugin-syntax-bigint",
"@babel/plugin-proposal-class-properties",
"@babel/plugin-proposal-optional-chaining"
]
}
29 changes: 19 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
{
"name": "snek-client",
"version": "0.1.1",
"version": "0.2.0",
"description": "Enoy it.",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "npx tsc"
"type-check": "tsc --noEmit",
"type-check:watch": "npm run type-check -- --watch",
"build": "npm run build:types && npm run build:js",
"build:types": "tsc --emitDeclarationOnly",
"build:js": "babel src --out-dir lib --extensions \".ts,.tsx\" --source-maps inline"
},
"files": [
"lib",
Expand All @@ -24,17 +27,23 @@
},
"homepage": "https://github.com/snek-at/client#readme",
"dependencies": {
"apollo-cache-inmemory": "^1.6.5",
"apollo-client": "^2.6.8",
"apollo-link-http": "^1.5.16",
"graphql": "^14.6.0",
"@apollo/client": "^3.1.5",
"@types/node": "^14.14.10",
"apollo-upload-client": "^14.1.1",
"graphql": "^14.7.0",
"graphql-tag": "^2.10.3",
"js-cookie": "^2.2.1"
},
"devDependencies": {
"@babel/core": "^7.9.6",
"@babel/plugin-proposal-class-properties": "^7.8.3",
"@babel/cli": "^7.12.8",
"@babel/core": "^7.12.9",
"@babel/plugin-proposal-class-properties": "^7.12.1",
"@babel/plugin-proposal-optional-chaining": "^7.12.7",
"@babel/plugin-syntax-bigint": "^7.8.3",
"@babel/preset-env": "^7.12.7",
"@babel/preset-typescript": "^7.12.7",
"@types/apollo-upload-client": "^14.1.0",
"@types/js-cookie": "^2.2.6",
"typescript": "^3.8.3"
"typescript": "^3.9.7"
}
}
6 changes: 6 additions & 0 deletions src/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"anonUser": {
"username": "cisco",
"password": "ciscocisco"
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
}
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a YAML file would be 100 times cooler. 💯

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In contrast to YAML, JSON can be imported as follows:

//> Config
import Config from "../../../config.json";

This notation guarantees full IntelliSense.


A method which works with YAML and JSON is as follows:

var config = require("../../../config.json")

But this notation does not have any IntelliSense support without implementing addidional interfaces.

Due to the small scope of the configuration this change would results in more work than benefits.

39 changes: 13 additions & 26 deletions src/endpoints/apollo.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
//#region > Imports
//#PACKAGE "apollo-client"
//## npm install "apollo-client"@2.6.8
// Contains the client for graphql handling
import { ApolloClient } from "apollo-client";
//#PACKAGE "apollo-link-http"
//## npm install "apollo-link-http"@1.5.16
// Contains the link for the apollo client
import { HttpLink } from "apollo-link-http";
//#PACKAGE "apollo-cache-inmemory"
//## npm install "apollo-cache-inmemory"@1.6.5
// Contains cache handling for apollo
//#PACKAGE "@apollo/client"
import {
ApolloClient,
ApolloLink,
InMemoryCache,
IntrospectionFragmentMatcher,
NormalizedCacheObject,
} from "apollo-cache-inmemory";
HttpLink,
} from "@apollo/client";
//#PACKAGE "'apollo-upload-client"
// Contains the link for the apollo client
import { createUploadLink } from "apollo-upload-client";
//#PACKAGE "graphql"
//## npm install "graphql"@14.6.0
// Contains the interface for gql queries, mutations and subscriptions
Expand All @@ -32,7 +27,7 @@ import { ApolloResult } from "./index";
/** @class Apollo client for graphql handling */
class Apollo implements ApolloEndpoint {
//> Fields
private link: HttpLink;
private link: ApolloLink;
private cache: InMemoryCache;
private client: ApolloClient<NormalizedCacheObject>;

Expand All @@ -49,26 +44,18 @@ class Apollo implements ApolloEndpoint {
*/
constructor(uri: string, options: Options) {
this.headers = options.headers;
const fragmentMatcher = new IntrospectionFragmentMatcher({
introspectionQueryResultData: {
__schema: {
types: [],
},
},
});

try {
this.cache = new InMemoryCache({ fragmentMatcher });
this.cache = new InMemoryCache();
} catch {
//#ERROR
throw new Error("An error occurred while initializing the cache!");
}

try {
this.link = new HttpLink({
uri,
headers: options.headers,
});
const uploadLink = createUploadLink({ uri, headers: options.headers });

this.link = ApolloLink.from([uploadLink]);
} catch {
//#ERROR
throw new Error("An error occurred while initializing the API link!");
Expand Down
55 changes: 50 additions & 5 deletions src/endpoints/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
//## npm install "graphql"@14.6.0
// Contains the interface for gql queries, mutations and subscriptions
import { DocumentNode } from "graphql";
import { FetchResult } from "apollo-link";
import { ApolloQueryResult } from "apollo-client";
import { ApolloQueryResult, FetchResult } from "@apollo/client";
//#endregion

//#region > Types
Expand Down Expand Up @@ -75,21 +74,21 @@ interface ScraperEndpoint extends Endpoint {
/**
* GetJson: A method which gets json data from a specific url.
*
* @param url A web url
* @param path Path to the endpoint
* @returns {Promise<T>} Json data in the given format <T>
*/
getJson<T>(url: string): Promise<T>;
/**
* GetDom: A method which gets DOM data from a specific url.
*
* @param url A web url
* @param path Path to the endpoint
* @returns {Promise<Document>} A DOM Document
*/
getDom(url: string): Promise<Document>;
/**
* Post: A method to post data to a specific url.
*
* @param {string} url A web url
* @param {string} path Path to the endpoint
* @param data Data which is filled into the body of a post request
* @returns {Promise<Document>} A DOM Document
*/
Expand All @@ -106,6 +105,52 @@ interface ScraperEndpoint extends Endpoint {
| null
| undefined
): Promise<T>;
/**
* Send fetch request to a endpoint and get the respective result.
*
* @param {string} path Path to the endpoint. Specify it like "/foo/bar".
* The correct placement of the slashes is essential!
* @param {"GET" | "POST" | "PUT" | "PATCH" | "DELETE"} type HTTP methods
* @param data Data which is filled into the body of a post request
* @returns {Promise<Response>} A DOM Document
*/
fetch(
path: string,
type: "GET" | "POST" | "PUT" | "PATCH" | "DELETE",
data?:
| string
| Blob
| ArrayBufferView
| ArrayBuffer
| FormData
| URLSearchParams
| ReadableStream<Uint8Array>
| null
| undefined
): Promise<Response>;
/**
* Send fetch request to a endpoint and get the respective JSON result.
*
* @param {string} path Path to the endpoint. Specify it like "/foo/bar".
* The correct placement of the slashes is essential!
* @param {"GET" | "POST" | "PUT" | "PATCH" | "DELETE"} type HTTP methods
* @param data Data which is filled into the body of a post request
* @returns {Promise<Response>} A DOM Document
*/
fetchJson<T>(
path: string,
type: "GET" | "POST" | "PUT" | "PATCH" | "DELETE",
data?:
| string
| Blob
| ArrayBufferView
| ArrayBuffer
| FormData
| URLSearchParams
| ReadableStream<Uint8Array>
| null
| undefined
): Promise<T>;
}
//#endregion

Expand Down
91 changes: 75 additions & 16 deletions src/endpoints/scraper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,22 +69,9 @@ class Scraper implements ScraperEndpoint {
* @returns {object} DOM object
*/
async getDom(path: string): Promise<Document> {
return fetch(this.url + path, {
headers: {
...this.headers,
},
})
.then((response) => {
if (!response.ok) {
//#ERROR
throw new Error(response.statusText);
}

return response.text();
})
.then((text) => {
return new DOMParser().parseFromString(text, "text/html");
});
const text = await (await this.fetch(path, "GET")).text();

return new DOMParser().parseFromString(text, "text/html");
}

/**
Expand Down Expand Up @@ -123,6 +110,78 @@ class Scraper implements ScraperEndpoint {
return response.json().then((data) => data as T);
});
}

/**
* Send fetch request to a endpoint and get the respective result.
*
* @param {string} path Path to the endpoint. Specify it like "/foo/bar".
* The correct placement of the slashes is essential!
* @param {"GET" | "POST" | "PUT" | "PATCH" | "DELETE"} type HTTP methods
* @param data Data which is filled into the body of a post request
* @returns {Promise<Response>} A DOM Document
*/
async fetch(
path: string,
type: "GET" | "POST" | "PUT" | "PATCH" | "DELETE",
data?:
| string
| Blob
| ArrayBufferView
| ArrayBuffer
| FormData
| URLSearchParams
| ReadableStream<Uint8Array>
| null
| undefined
): Promise<Response> {
return fetch(this.url + path, {
method: type,
body: data,
headers: {
...this.headers,
},
}).then(async (response) => {
if (!response.ok) {
//#ERROR
throw new Error(response.statusText);
}

return response;
});
}

/**
* Send fetch request to a endpoint and get the respective JSON result.
*
* @param {string} path Path to the endpoint. Specify it like "/foo/bar".
* The correct placement of the slashes is essential!
* @param {"GET" | "POST" | "PUT" | "PATCH" | "DELETE"} type HTTP methods
* @param data Data which is filled into the body of a post request
* @returns {Promise<Response>} A DOM Document
*/
async fetchJson<T>(
path: string,
type: "GET" | "POST" | "PUT" | "PATCH" | "DELETE",
data?:
| string
| Blob
| ArrayBufferView
| ArrayBuffer
| FormData
| URLSearchParams
| ReadableStream<Uint8Array>
| null
| undefined
): Promise<T> {
return this.fetch(path, type, data).then(async (response) => {
if (!response.ok) {
//#ERROR
throw new Error(response.statusText);
}

return response.json().then((data) => data as T);
});
}
}
//#endregion

Expand Down
Loading