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 21 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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "snek-client",
"version": "0.1.1",
"version": "0.2.0",
"description": "Enoy it.",
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand Down
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.

2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class SnekClient extends Client {
* @param type A type description to differ between multiple instances
*/
constructor(
url: string = "https://engine.snek.at/api/graphiql",
url: string = "https://engine.snek.at/graphql",
headers: object = {},
type: string = "graphql"
) {
Expand Down
25 changes: 17 additions & 8 deletions src/session/sessions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import SnekTasks from "../templates/snek/gql/tasks/index";
import { ApolloEndpoint } from "../endpoints/index";
// Contains basic session interfaces
import { User } from "./index";
//> Config
import Config from "../config.json";
//#endregion

//#region > Classes
Expand Down Expand Up @@ -206,31 +208,38 @@ class SnekSession extends CookieSession {
* @returns {Promise<any>} A UserData object
*/
async begin(user?: User): Promise<any> {
let response;
let anonymous = false;

if (!user && this.refreshToken) {
/* Refresh token and retrieve a new refreshToken if necessary */
this.refresh();
} else {
let authData;

if (!user) {
/* Authenticate anonymous user */
response = await this.tasks.auth.anon();
authData = (await this.tasks.auth.anon()).data?.auth;
anonymous = true;
} else {
/* Authenticate real user */
response = await this.tasks.auth.nonanon(user);
authData = (await this.tasks.auth.nonanon(user)).data?.auth;
}

/* Set tokens */
this.token = response.data?.auth.token;
this.refreshToken = response.data?.auth.refreshToken;
this.token = authData?.token;
this.refreshToken = authData?.refreshToken;

return response.data?.auth.user;
return { anonymous, ...authData?.user };
}

/* Get user data */
response = await this.tasks.user.whoami();
const userData = (await this.tasks.user.whoami()).data?.whoami;

if (userData?.username === Config.anonUser.username) {
anonymous = true;
}

return response.data?.whoami;
return { anonymous, ...userData };
}

/**
Expand Down
5 changes: 0 additions & 5 deletions src/templates/snek/gql/mutations/jwtAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@ const auth = gql`
refreshToken
user {
username
firstName
lastName
email
dateJoined
lastLogin
}
}
}
Expand Down
19 changes: 9 additions & 10 deletions src/templates/snek/gql/mutations/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,16 @@ import gql from "graphql-tag";
*/
const registration = gql`
mutation registration($token: String!, $values: GenericScalar!) {
registration: registrationRegistrationFormPage(
token: $token
url: "/registration"
values: $values
) {
result
errors {
name
errors
registration: registrationFormPage(
url: "/registration/",
token: $token,
values: $values) {
result
errors {
name
errors
}
}
}
}
`;

Expand Down
28 changes: 23 additions & 5 deletions src/templates/snek/gql/queries/general.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ import gql from "graphql-tag";
*/
const gitlabServer = gql`
query gitLabServers($token: String!) {
page(url: "/registration", token: $token) {
... on RegistrationRegistrationFormPage {
page(slug: "registration", token: $token) {
... on RegistrationFormPage {
supportedGitlabs {
... on RegistrationGitlab_Server {
... on Gitlab_Server {
id
organisation
domain
field
}
}
}
Expand All @@ -43,10 +43,28 @@ const allPageUrls = gql`
}
}
`;

/**
* List of user page urls.
*
* @param {string} JWT A users JWT
* @returns {string} A serialized JSON object with a list of all page urls
* @description A query to fetch all user page urls
*/
const allUserPageUrls = gql`
query userPages($token: String!) {
page(slug: "user", token: $token) {
children {
slug
title
}
}
}
`;
//#endregion

//#region > Exports
export { gitlabServer, allPageUrls };
export { gitlabServer, allPageUrls, allUserPageUrls };
//#endregion

/**
Expand Down
13 changes: 4 additions & 9 deletions src/templates/snek/gql/queries/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,22 @@ const whoami = gql`
query whoami($token: String!) {
whoami: me(token: $token) {
username
firstName
lastName
email
dateJoined
lastLogin
}
}
`;

/**
* Get user profile.
*
* @param {string} url PageUrl of a user profile
* @param {string} slug Slug: <user_<username>>
* @param {string} token A users JWT
* @returns {string} A profile page of a user
* @description A query to fetch profile data
*/
const profile = gql`
query profile($url: String!, $token: String!) {
profile: page(url: $url, token: $token) {
... on ProfileProfilePage {
query profile($slug: String!, $token: String!) {
profile: page(slug: $slug, token: $token) {
... on ProfilePage {
username
firstName
lastName
Expand Down
6 changes: 4 additions & 2 deletions src/templates/snek/gql/tasks/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { User } from "../../../../session";
//> Types
// Contains the type declarations for Apollo results
import { ApolloResult } from "./index";
//> Config
import Config from "../../../../config.json";
//#endregion

//#region > Interfaces
Expand Down Expand Up @@ -68,8 +70,8 @@ class SnekGqlAuthTasks {
"mutation",
this.parent.template.mutations.jwtAuth.auth,
{
username: "cisco",
password: "ciscocisco",
username: Config.anonUser.username,
password: Config.anonUser.password,
}
);

Expand Down
33 changes: 30 additions & 3 deletions src/templates/snek/gql/tasks/general.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { ApolloResult } from "./index";
*/
interface GitlabServerData {
page: {
supportedGitlabs: [];
supportedGitlabs: { id: string; organisation: string; domain: string; }[];
};
}

Expand All @@ -23,7 +23,15 @@ interface GitlabServerData {
* data.
*/
interface AllPageUrlData {
pages: [];
pages: { urlPath: string }[];
}

/**
* @interface AllUserPageUrlsData defines the types of the response of the
* allUserPageUrls query.
*/
interface AllUserPageUrlsData {
page: { children: { url: string }[] };
}
//#endregion

Expand All @@ -36,7 +44,7 @@ class SnekGqlGeneralTasks {
* @author Nico Schett <contact@schett.net>
* @param {string} parent The parent task
*/
constructor(private parent: SnekTasks) {}
constructor(private parent: SnekTasks) { }

/**
* Gitlab Server.
Expand Down Expand Up @@ -73,6 +81,25 @@ class SnekGqlGeneralTasks {

return response;
}

/**
* All user page urls.
*
* @returns {Promise<ApolloResult<AllUserPageUrlsData>>} A list of all user
* page urls.
* @description Get a list of all pages
*/
async allUserPageUrls(): Promise<ApolloResult<AllUserPageUrlsData>> {
const response = await this.parent.run<AllUserPageUrlsData>(
"query",
this.parent.template.queries.general.allUserPageUrls,
{
token: await this.parent.session.upToDateToken(),
}
);

return response;
}
}
//#endregion

Expand Down
6 changes: 3 additions & 3 deletions src/templates/snek/gql/tasks/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,15 @@ class SnekGqlUserTasks {
/**
* Get profile.
*
* @param {string} url A url of a page
* @param {string} slug Slug: <user_<username>>
* @returns {Promise<ApolloResult<ProfileData>>} The profile page of a user
*/
async profile(url: string): Promise<ApolloResult<ProfileData>> {
async profile(slug: string): Promise<ApolloResult<ProfileData>> {
const response = await this.parent.run<ProfileData>(
"query",
this.parent.template.queries.user.profile,
{
url,
slug,
token: await this.parent.session.upToDateToken(),
}
);
Expand Down