From 7f310b48a3d46b4c8f9a1e57072e33a3bcd31f95 Mon Sep 17 00:00:00 2001 From: Tyler Barker Date: Sat, 7 Dec 2024 17:02:53 +1100 Subject: [PATCH] various and sundry changes --- .gitignore | 2 +- LICENSE.md | 24 ++++++++++++ README.md | 41 ++------------------- build.mjs | 2 +- package.json | 8 ++-- packages/phoenix_ts/LICENSE.md | 23 ++++++++++++ packages/phoenix_ts/README.md | 23 ++++++++++++ packages/phoenix_ts/api-extractor.json | 4 +- packages/phoenix_ts/mix.exs | 37 ++++++++++++++----- packages/phoenix_ts/package.json | 8 ++-- packages/phoenix_ts/test/channel.test.js | 2 +- packages/phoenix_ts/test/presence.test.js | 2 +- packages/phoenix_ts/test/serializer.test.js | 2 +- packages/phoenix_ts/test/socket.test.js | 2 +- packages/phoenix_ts/tsconfig.json | 2 +- 15 files changed, 120 insertions(+), 62 deletions(-) create mode 100644 LICENSE.md create mode 100644 packages/phoenix_ts/LICENSE.md create mode 100644 packages/phoenix_ts/README.md diff --git a/.gitignore b/.gitignore index cdec25f..106b457 100644 --- a/.gitignore +++ b/.gitignore @@ -4,7 +4,7 @@ phoenix.d.ts # Build directory -priv/ +assets/ # direnv .envrc diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..502eb46 --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,24 @@ +# MIT License + +Copyright (c) 2014 Chris McCord ([phoenix](https://github.com/phoenixframework/phoenix)) +Copyright (c) 2018 Chris McCord ([phoenix_live_view](https://github.com/phoenixframework/phoenix_live_view)) +Copyright (c) 2024 Tyler Barker - TypeScript ports + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md index d6f7515..98385d6 100644 --- a/README.md +++ b/README.md @@ -1,43 +1,10 @@ -# phoenix_ts +# phx_ts -`phoenix_ts` is, first and foremost, an experiment and a learning exercise. It's an attempt to port the JS library bundled with the Phoenix web framework (as of v1.7.14) to TypeScript. Bundled and tested with [Bun](https://bun.sh). +`phx_ts` is an experiment and a learning exercise. It's an attempt to port the JS libraries bundled with the Phoenix & Phoenix LiveView package to TypeScript such that you can install them via Hex, update your `package.json` and get all the resulting types with no change in functionality. Bundled and tested with [Bun](https://bun.sh). -I'll be doing the same for `phoenix_live_view` in a separate repo at a later date. Great timing actually, as they just announced [a release candidate for v1.0](https://github.com/phoenixframework/phoenix_live_view/commit/d84b19c9761c8a665084a05178dfbd6de8acd6e8)! +I'll be doing the same for `phoenix_live_view` as the 2nd package in this monorepo soon. Both will be published to Hex independently of one another. -This effort isn't officially endorsed by the Phoenix team, just a bit of fun. That being said, I would love it if this or a similar effort was eventually integrated into the Phoenix project. Personally, I think it'd be easier to maintain, extend, and consume the framework if the JS clients were written in TypeScript and shipped types. - -## TODO - -- [x] Port the Mocha test suite to Bun test (3x speedup!) -- [x] Port util.js to TypeScript -- [x] Port constants.js to TypeScript -- [x] Port index.js to TypeScript -- [x] Port ajax.js to TypeScript -- [x] Port timer.js to TypeScript -- [x] Port serializer.js to TypeScript -- [x] Port push.js to TypeScript -- [x] Port longpoll.js to TypeScript -- [ ] Port presence.js to TypeScript -- [ ] Port channel.js to TypeScript -- [ ] Port socket.js to TypeScript -- [ ] Circle back to `any` types after everything is ported -- [ ] Circle back to `as` type assertions after everything is ported -- [ ] Reassess bundling targets e.g what do we need to support? -- [ ] Configure as Hex package (minimal Elixir scaffolding) -- [ ] Write installation documentation - -## Staying Up to Date - -Current Version: v1.7.14 - -I intend to release a corresponding version of this library for each Phoenix release from v1.7.12 onwards, even if the bundled JS does not change to assure compatibility. - -I've written a script - to be run every day by a GH Action - which: - -1. Checks the Phoenix repo for releases newer than our current version. -2. If a new release is found, it raises an issue on this repo with details of any changes to the Phoenix `assets/` repo if they are present. - -See `phx-changes-check.ts`. +This isn't officially endorsed by the Phoenix team. I'm doing it because it's fun, and I think it'd be easier to maintain, extend, and consume the framework if the JS clients were written in TypeScript and shipped types. ## Development diff --git a/build.mjs b/build.mjs index d905ad6..646a39f 100644 --- a/build.mjs +++ b/build.mjs @@ -2,7 +2,7 @@ import Bun from "bun"; const phoenixTsResult = await Bun.build({ entrypoints: ["./packages/phoenix_ts/src/index.ts"], - outdir: "./packages/phoenix_ts/priv/assets/js/phoenix", + outdir: "./packages/phoenix_ts/assets/js/phoenix", minify: true, target: "browser", }); diff --git a/package.json b/package.json index e8516a4..bdf72ef 100644 --- a/package.json +++ b/package.json @@ -1,14 +1,16 @@ { "name": "phx_ts", "version": "0.0.0", - "workspaces": ["packages/*"], + "workspaces": [ + "packages/*" + ], "devDependencies": { "@microsoft/api-extractor": "^7.43.2", "bun-types": "^1.1.38" }, "scripts": { "build:lib": "bun build.mjs", - "build:types": "cd ./packages/phoenix_ts && bunx tsc && bunx api-extractor run --local", - "build": "bun run build:lib && bun run build:types" + "build:phoenix_ts:types": "cd ./packages/phoenix_ts && bunx tsc && bunx api-extractor run --local", + "build": "bun run build:lib && bun run build:phoenix_ts:types" } } diff --git a/packages/phoenix_ts/LICENSE.md b/packages/phoenix_ts/LICENSE.md new file mode 100644 index 0000000..7bb459f --- /dev/null +++ b/packages/phoenix_ts/LICENSE.md @@ -0,0 +1,23 @@ +# MIT License + +Copyright (c) 2014 Chris McCord ([phoenix](https://github.com/phoenixframework/phoenix)) +Copyright (c) 2024 Tyler Barker - TypeScript port + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/packages/phoenix_ts/README.md b/packages/phoenix_ts/README.md new file mode 100644 index 0000000..de904a3 --- /dev/null +++ b/packages/phoenix_ts/README.md @@ -0,0 +1,23 @@ +# phoenix_ts + +Current Version: v1.7.17 + +## TODO + +- [x] Port the Mocha test suite to Bun test (3x speedup!) +- [x] Port util.js to TypeScript +- [x] Port constants.js to TypeScript +- [x] Port index.js to TypeScript +- [x] Port ajax.js to TypeScript +- [x] Port timer.js to TypeScript +- [x] Port serializer.js to TypeScript +- [x] Port push.js to TypeScript +- [x] Port longpoll.js to TypeScript +- [ ] Port presence.js to TypeScript +- [ ] Port channel.js to TypeScript +- [ ] Port socket.js to TypeScript +- [x] Configure as Hex package (minimal Elixir scaffolding) +- [ ] Circle back to `any` types after everything is ported +- [ ] Circle back to `as` type assertions after everything is ported +- [ ] Reassess bundling targets e.g what do we need to support? +- [ ] Write installation documentation diff --git a/packages/phoenix_ts/api-extractor.json b/packages/phoenix_ts/api-extractor.json index c45623d..0133c63 100644 --- a/packages/phoenix_ts/api-extractor.json +++ b/packages/phoenix_ts/api-extractor.json @@ -1,7 +1,7 @@ { "extends": "../../api-extractor.base.json", "projectFolder": ".", - "mainEntryPointFilePath": "/priv/assets/js/phoenix/index.d.ts", + "mainEntryPointFilePath": "/assets/js/phoenix/index.d.ts", "dtsRollup": { /** * (REQUIRED) Whether to generate the .d.ts rollup file. @@ -20,7 +20,7 @@ * SUPPORTED TOKENS: , , * DEFAULT VALUE: "/dist/.d.ts" */ - "untrimmedFilePath": "/priv/assets/js/phoenix/index.d.ts" + "untrimmedFilePath": "/assets/js/phoenix/index.d.ts" /** * Specifies the output path for a .d.ts rollup file to be generated with trimming for an "alpha" release. diff --git a/packages/phoenix_ts/mix.exs b/packages/phoenix_ts/mix.exs index b7a0f4f..9abb16a 100644 --- a/packages/phoenix_ts/mix.exs +++ b/packages/phoenix_ts/mix.exs @@ -2,21 +2,40 @@ defmodule PhoenixTS.MixProject do use Mix.Project @app :phoenix_ts - @version "1.7.14" + @version "1.7.17" def project do [ app: @app, version: @version, - elixir: "~> 1.15", - description: "The unofficial TypeScript client for the Phoenix web framework.", + elixir: "~> 1.13", deps: [], - package: [ - name: "phoenix_ts", - files: ~w(mix.exs README.md priv), - licenses: ["MIT"], - links: %{"GitHub" => "https://github.com/tylerbarker/phx_ts"} - ] + docs: docs(), + package: packages(), + description: """ + The unofficial TypeScript client for the Phoenix web framework. + """ + ] + end + + defp docs() do + [ + main: "readme", + name: "PhoenixTS", + source_ref: "v#{@version}", + canonical: "http://hexdocs.pm/phoenix_ts", + source_url: "https://github.com/tylerbarker/phx_ts", + extras: ["README.md"] + ] + end + + def package do + [ + name: "phoenix_ts", + maintainers: ["Tyler Barker"], + licenses: ["MIT"], + links: %{"GitHub" => "https://github.com/tylerbarker/phx_ts"}, + files: ~w(mix.exs lib assets README.md LICENSE.md) ] end end diff --git a/packages/phoenix_ts/package.json b/packages/phoenix_ts/package.json index b8569f2..c712691 100644 --- a/packages/phoenix_ts/package.json +++ b/packages/phoenix_ts/package.json @@ -1,11 +1,11 @@ { "name": "phoenix_ts", - "version": "1.7.14", + "version": "1.7.17", "description": "The unofficial TypeScript client for the Phoenix web framework.", "license": "MIT", - "main": "./priv/assets/js/phoenix/index.js", - "module": "./priv/assets/js/phoenix/index.js", - "typings": "./priv/assets/js/phoenix/index.d.ts", + "main": "./assets/js/phoenix/index.js", + "module": "./assets/js/phoenix/index.js", + "typings": "./assets/js/phoenix/index.d.ts", "type": "module", "repository": { "type": "git", diff --git a/packages/phoenix_ts/test/channel.test.js b/packages/phoenix_ts/test/channel.test.js index d3a4b49..39865c2 100644 --- a/packages/phoenix_ts/test/channel.test.js +++ b/packages/phoenix_ts/test/channel.test.js @@ -1,7 +1,7 @@ import { afterEach, beforeAll, beforeEach, describe, expect, it } from "bun:test"; import sinon from "sinon"; -import { Channel, Socket } from "../priv/assets/js/phoenix"; +import { Channel, Socket } from "../assets/js/phoenix"; let channel, socket; diff --git a/packages/phoenix_ts/test/presence.test.js b/packages/phoenix_ts/test/presence.test.js index 94258d9..158c6cf 100644 --- a/packages/phoenix_ts/test/presence.test.js +++ b/packages/phoenix_ts/test/presence.test.js @@ -1,6 +1,6 @@ import { describe, expect, it } from "bun:test"; -import { Presence } from "../priv/assets/js/phoenix"; +import { Presence } from "../assets/js/phoenix"; const clone = (obj) => { const cloned = JSON.parse(JSON.stringify(obj)); diff --git a/packages/phoenix_ts/test/serializer.test.js b/packages/phoenix_ts/test/serializer.test.js index e25a829..085bf6d 100644 --- a/packages/phoenix_ts/test/serializer.test.js +++ b/packages/phoenix_ts/test/serializer.test.js @@ -1,6 +1,6 @@ import { describe, expect, it } from "bun:test"; -import { Serializer } from "../priv/assets/js/phoenix"; +import { Serializer } from "../assets/js/phoenix"; const exampleMsg = { join_ref: "0", diff --git a/packages/phoenix_ts/test/socket.test.js b/packages/phoenix_ts/test/socket.test.js index 0e427fe..49edb9c 100644 --- a/packages/phoenix_ts/test/socket.test.js +++ b/packages/phoenix_ts/test/socket.test.js @@ -10,7 +10,7 @@ import { import sinon from "sinon"; import { WebSocket, Server as WebSocketServer } from "mock-socket"; import { encode } from "./serializer"; -import { Socket, LongPoll } from "../priv/assets/js/phoenix"; +import { Socket, LongPoll } from "../assets/js/phoenix"; let socket; diff --git a/packages/phoenix_ts/tsconfig.json b/packages/phoenix_ts/tsconfig.json index 08bd92e..cff5456 100644 --- a/packages/phoenix_ts/tsconfig.json +++ b/packages/phoenix_ts/tsconfig.json @@ -2,6 +2,6 @@ "extends": "../../base.jsonc", "include": ["./src/**/*"], "compilerOptions": { - "outDir": "priv/assets/js/phoenix" + "outDir": "assets/js/phoenix" } }