Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
dmmulroy committed Mar 19, 2024
1 parent 81c826d commit a508cb1
Show file tree
Hide file tree
Showing 11 changed files with 276 additions and 15 deletions.
175 changes: 175 additions & 0 deletions oauth/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
# Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore

# Logs

logs
_.log
npm-debug.log_
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*

# Caches

.cache

# Diagnostic reports (https://nodejs.org/api/report.html)

report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json

# Runtime data

pids
_.pid
_.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover

lib-cov

# Coverage directory used by tools like istanbul

coverage
*.lcov

# nyc test coverage

.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)

.grunt

# Bower dependency directory (https://bower.io/)

bower_components

# node-waf configuration

.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)

build/Release

# Dependency directories

node_modules/
jspm_packages/

# Snowpack dependency directory (https://snowpack.dev/)

web_modules/

# TypeScript cache

*.tsbuildinfo

# Optional npm cache directory

.npm

# Optional eslint cache

.eslintcache

# Optional stylelint cache

.stylelintcache

# Microbundle cache

.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history

.node_repl_history

# Output of 'npm pack'

*.tgz

# Yarn Integrity file

.yarn-integrity

# dotenv environment variable files

.env
.env.development.local
.env.test.local
.env.production.local
.env.local

# parcel-bundler cache (https://parceljs.org/)

.parcel-cache

# Next.js build output

.next
out

# Nuxt.js build / generate output

.nuxt
dist

# Gatsby files

# Comment in the public line in if your project uses Gatsby and not Next.js

# https://nextjs.org/blog/next-9-1#public-directory-support

# public

# vuepress build output

.vuepress/dist

# vuepress v2.x temp and cache directory

.temp

# Docusaurus cache and generated files

.docusaurus

# Serverless directories

.serverless/

# FuseBox cache

.fusebox/

# DynamoDB Local files

.dynamodb/

# TernJS port file

.tern-port

# Stores VSCode versions used for testing VSCode extensions

.vscode-test

# yarn v2

.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*

# IntelliJ based IDEs
.idea

# Finder (MacOS) folder config
.DS_Store
15 changes: 15 additions & 0 deletions oauth/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# oauth

To install dependencies:

```bash
bun install
```

To run:

```bash
bun run index.ts
```

This project was created using `bun init` in bun v1.0.33. [Bun](https://bun.sh) is a fast all-in-one JavaScript runtime.
Binary file added oauth/bun.lockb
Binary file not shown.
20 changes: 20 additions & 0 deletions oauth/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import Elysia from "elysia";

const app = new Elysia()
.get("/twitch/oauth", async (ctx) => {
console.log(JSON.stringify(ctx.query));
})
.listen(3030);

console.log(
`🦊 Elysia is running at ${app.server?.hostname}:${app.server?.port}`,
);

const url = `
https://id.twitch.tv/oauth2/authorize
?response_type=code
&client_id=cew8p1bv247ua1czt6a1okon8ejy1r
&redirect_uri=http://localhost:3030/twitch/oauth
&scope=user%3Awrite%3Achat+user%3Abot+channel%3Abot
&state=foobar
`;
14 changes: 14 additions & 0 deletions oauth/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "oauth",
"module": "index.ts",
"type": "module",
"devDependencies": {
"@types/bun": "latest"
},
"peerDependencies": {
"typescript": "^5.0.0"
},
"dependencies": {
"elysia": "^1.0.5"
}
}
27 changes: 27 additions & 0 deletions oauth/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"compilerOptions": {
// Enable latest features
"lib": ["ESNext"],
"target": "ESNext",
"module": "ESNext",
"moduleDetection": "force",
"jsx": "react-jsx",
"allowJs": true,

// Bundler mode
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"verbatimModuleSyntax": true,
"noEmit": true,

// Best practices
"strict": true,
"skipLibCheck": true,
"noFallthroughCasesInSwitch": true,

// Some stricter flags (disabled by default)
"noUnusedLocals": false,
"noUnusedParameters": false,
"noPropertyAccessFromIndexSignature": false
}
}
12 changes: 6 additions & 6 deletions src/glitch.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ pub fn main() {
let client_options = Options(client_id: client_id, access_token: access_token)
let client = client.new(client_options)

let get_users_request =
GetUsersRequest(user_ids: None, user_logins: Some(["dmmulroy"]))

let result = user.get_users(client, get_users_request)

pprint.debug(result)
// let get_users_request =
// GetUsersRequest(user_ids: None, user_logins: Some(["dmmulroy"]))
//
// let result = user.get_users(client, get_users_request)
//
// pprint.debug(result)

let send_message_request =
SendMessageRequest(
Expand Down
6 changes: 4 additions & 2 deletions src/glitch/api/api_response.gleam
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import gleam/dynamic.{type Decoder}
import gleam/io
import gleam/json
import gleam/http/response.{type Response}
import glitch/extended/function_ext
Expand All @@ -7,14 +8,15 @@ pub type TwitchApiResponse(data) {
TwitchApiResponse(data: List(data))
}

fn decoder(of data_decoder: Decoder(data)) -> Decoder(TwitchApiResponse(data)) {
fn decoder(data_decoder: Decoder(data)) -> Decoder(TwitchApiResponse(data)) {
dynamic.decode1(
TwitchApiResponse,
dynamic.field("data", dynamic.list(of: data_decoder)),
)
}

pub fn from_json(json_string: String, of data_decoder: Decoder(data)) {
pub fn from_json(json_string: String, data_decoder: Decoder(data)) {
io.println(json_string)
json.decode(json_string, decoder(data_decoder))
}

Expand Down
13 changes: 8 additions & 5 deletions src/glitch/api/chat.gleam
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import gleam/dynamic.{type Decoder}
import gleam/io
import gleam/option.{type Option}
import gleam/result
import gleam/json.{type DecodeError, type Json}
Expand Down Expand Up @@ -38,11 +39,11 @@ fn send_message_request_to_json(request: SendMessageRequest) -> Json {
#("broadcaster_id", json.string(request.broadcaster_id)),
#("sender_id", json.string(request.sender_id)),
#("message", json.string(request.message)),
#(
"reply_parent_message_id",
json_ext.option(request.reply_parent_message_id, json.string),
),
])
// #(
// "reply_parent_message_id",
// json_ext.option(request.reply_parent_message_id, json.string),
// ),
}

pub type SendMessageError {
Expand All @@ -54,6 +55,8 @@ pub fn send_message(
client: Client,
request: SendMessageRequest,
) -> Result(Response(List(Message)), SendMessageError) {
let body = send_message_request_to_json(request)
io.println(json.to_string(body))
let request =
request.new()
|> request.set_body(send_message_request_to_json(request))
Expand All @@ -66,7 +69,7 @@ pub fn send_message(
)

response
|> response.try_map(api_response.from_json(_, of: message_decoder()))
|> response.try_map(api_response.from_json(_, message_decoder()))
|> result.try(api_response.get_data_from_response)
|> result.map_error(DecodeError)
}
7 changes: 6 additions & 1 deletion src/glitch/api/client.gleam
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import gleam/dynamic.{type Dynamic}
import gleam/io
import gleam/list
import gleam/pair
import gleam/http.{type Header, Get, Post}
Expand Down Expand Up @@ -34,7 +35,11 @@ pub fn headers(client: Client) -> List(Header) {
let client_id = client_id(client)
let access_token = "Bearer " <> access_token(client)

[#("Client-Id", client_id), #("Authorization", access_token)]
[
#("Authorization", access_token),
#("Client-Id", client_id),
#("content-type", "application/json"),
]
}

fn merge_headers(
Expand Down
2 changes: 1 addition & 1 deletion src/glitch/api/user.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ pub fn get_users(
)

response
|> response.try_map(api_response.from_json(_, of: decoder()))
|> response.try_map(api_response.from_json(_, decoder()))
|> result.try(api_response.get_data_from_response)
|> result.map_error(DecodeError)
}

0 comments on commit a508cb1

Please sign in to comment.