Skip to content

Commit dff6efc

Browse files
committed
feat: ✨ robust base, modularity
1 parent 692e6d1 commit dff6efc

File tree

18 files changed

+421
-2
lines changed

18 files changed

+421
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
main.ts

.vscode/settings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"conventionalCommits.scopes": [
33
"gh_actions"
4-
]
4+
],
5+
"deno.enable": true
56
}

deno.jsonc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"tasks": {
3+
// Dev
4+
"dev": "deno run -A --watch main.ts"
5+
}
6+
}

deno.lock

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

jsr.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"name": "@xlsoftware/smartshell-sdk",
3-
"version": "0.1.4",
3+
"version": "0.2.0",
44
"exports": "./mod.ts"
55
}

mod.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { Shell } from './src/shell.ts'
2+
import { gql } from './src/utils/syntax.ts'
3+
export {
4+
Shell,
5+
gql
6+
}

readme.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,40 @@
2323
<h2 id="install"><strong>🔗 Useful Links</strong></h2>
2424

2525
* <a href="#license">License</a>
26+
* <a href="#install">Installation</a>
27+
* <a href="https://marketplace.visualstudio.com/items?itemName=GraphQL.vscode-graphql-syntax">GraphQL: Syntax Highlighting for VS Code</a>
28+
29+
<h2 id="license"><strong>💾 Installation</strong></h2>
30+
31+
```
32+
// Node
33+
npx jsr add @xlsoftware/smartshell-sdk
34+
import { Shell } from "@xlsoftware/smartshell-sdk";
35+
36+
// Deno
37+
deno add @xlsoftware/smartshell-sdk
38+
import { Shell } from "@xlsoftware/smartshell-sdk";
39+
// OR
40+
import { Shell } from "https://deno.land/x/smartshell_sdk/mod.ts"
41+
```
42+
43+
<h2 id="license"><strong>📄 Quickstart</strong></h2>
44+
45+
```ts
46+
const shell = await new Shell({
47+
credentials: { login: '79998887766', password: 'password123' }
48+
})
49+
```
50+
51+
<h2 id="license"><strong>📄 API coverage</strong></h2>
52+
53+
The project started quite a long time ago is moving to a new code base and JSR registry
54+
55+
Due to the addition of new methods and the possibility of optimization the SDK project has received a second life and will continue to develop.
56+
57+
Status: **0.1% coverage**
58+
59+
Now in priority robust base and modularity
2660

2761
<h2 id="license"><strong>📜 License</strong></h2>
2862

src/api.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import type { ShellApiClub, ShellApiOptions, ShellApiEndpoint } from './types/api.ts'
2+
import type { GraphQLResponse } from './types/graphql.ts'
3+
import { ShellSdkQuery } from "./types/sdk.ts";
4+
import { ShellApiError, ShellSdkError } from "./utils/errors.ts";
5+
import { query, mutation } from './graphql/index.ts'
6+
7+
8+
/**
9+
* # class `ShellApi`
10+
*
11+
* Contains core methods of GraphQL API
12+
*
13+
* `@xlsoftware/smartshell-sdk/api`
14+
*/
15+
export class ShellApi {
16+
17+
private _endpoint: ShellApiEndpoint = `https://billing.smartshell.gg/api/graphql`
18+
private _clubs: ShellApiClub[] = []
19+
20+
constructor(private options: ShellApiOptions) {
21+
if (options.host) this._endpoint = `https://${options.host}.smartshell.gg/api/graphql`
22+
}
23+
24+
async init(): Promise<ShellApiClub[]> {
25+
if (this._clubs.length !== 0) throw new ShellSdkError('SDK can`t be initialized more than once!')
26+
const { login, password } = this.options.credentials
27+
if (!login || !password) throw new ShellSdkError('No credentials provided')
28+
const clubs_array = await query.userClubs(this, { login, password }, true)
29+
const clubs = clubs_array.data!.userClubs
30+
for (let i = 0; i < clubs.length; i++) {
31+
const id = clubs[i].id;
32+
if (this._clubs.some(token => token.id === id)) continue
33+
const tokens = await mutation.login(this, { login, password, company_id: id }, true)
34+
const data = tokens.data!.login
35+
this._clubs.push({ id, access_token: data.access_token, refresh_token: data.refresh_token, expires: Date.now() + (data.expires_in * 1000)})
36+
}
37+
return this._clubs
38+
39+
//! НАДО ДОДЕЛАТЬ РЕФРЕШ
40+
}
41+
42+
async request<T>(query: ShellSdkQuery, token: string): Promise<GraphQLResponse<T>> {
43+
const options = {
44+
method: "POST",
45+
headers: {
46+
"content-type": "application/json",
47+
...token ? { 'Authorization': `Bearer ${token}` } : {},
48+
},
49+
body: JSON.stringify({ query })
50+
}
51+
const response = await fetch('https://billing.smartshell.gg/api/graphql', options);
52+
const json: GraphQLResponse<T> = await response.json();
53+
if (json.errors) throw new ShellApiError(JSON.stringify(json.errors))
54+
return json
55+
}
56+
57+
58+
}

src/graphql/index.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import userClubs from './query/userClubs.ts'
2+
3+
4+
export const query = {
5+
userClubs
6+
}
7+
8+
import login from './mutation/login.ts'
9+
10+
export const mutation = {
11+
login
12+
}

src/graphql/mutation/login.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//@ts-nocheck <>
2+
import { gql } from "../../utils/syntax.ts";
3+
import { ShellContext, ShellSdkModule } from "../../types/sdk.ts"
4+
5+
interface QueryInput {
6+
login: string
7+
password: string
8+
company_id?: number
9+
}
10+
11+
interface QueryResponse {
12+
token_type: string
13+
expires_in: number
14+
access_token: string
15+
refresh_token: string
16+
}
17+
18+
const module: ShellSdkModule = async <TI,TR>(ctx: ShellContext, data: TI) => { return await ctx.request<TR>(gql`
19+
20+
mutation Login {
21+
login(input: { login: "${data.login}", password: "${data.password}", company_id: ${data.company_id} }) {
22+
token_type
23+
expires_in
24+
access_token
25+
refresh_token
26+
}
27+
}
28+
29+
`)}
30+
31+
export default module<QueryInput, QueryResponse>

0 commit comments

Comments
 (0)