@small-web/kitten-types 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +13 -0
- package/README.md +136 -0
- package/global.d.ts +288 -0
- package/index.d.ts +43 -0
- package/package.json +54 -0
- package/polka/index.d.ts +111 -0
- package/slugify/index.d.ts +246 -0
- package/types.d.ts +596 -0
- package/ws/index.d.ts +458 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
|
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
6
|
+
|
|
7
|
+
## [1.0.0] - 2023-04-14
|
|
8
|
+
|
|
9
|
+
__Initial release__
|
|
10
|
+
|
|
11
|
+
The successor to this module is Kitten Globals ([@small-web/kitten](https://codeberg.org/kitten/globals)), which has now been deprecated.
|
|
12
|
+
|
|
13
|
+
For previous history, please see that module.
|
package/README.md
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
# Kitten types
|
|
2
|
+
|
|
3
|
+
Type-safety for [Kitten](https://kitten.small-web.org) apps and sites.
|
|
4
|
+
|
|
5
|
+
This is a **types-only** package. It declares the global `kitten` namespace (via `declare global`) and exports reusable Kitten types you can use to annotate your own code.
|
|
6
|
+
|
|
7
|
+
> 💡 The older [`@small-web/kitten`](https://www.npmjs.com/package/@small-web/kitten) module [has now been deprecated](#relationship-to-small-web-kitten). You should use this module for new projects and plan on migrating existing projects over to it going forward.
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
```shell
|
|
12
|
+
npm install --save-dev @small-web/kitten-types
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Use
|
|
16
|
+
|
|
17
|
+
Kitten exposes a global `kitten` namespace at runtime.
|
|
18
|
+
|
|
19
|
+
This makes it easy to get up and running when learning, prototyping, or just building something quick without having to worry about type safety (you don’t have to import any packages to get started with building Kitten apps).
|
|
20
|
+
|
|
21
|
+
Here’s a quick Hello World example that greets you with a smiley face:
|
|
22
|
+
|
|
23
|
+
```js
|
|
24
|
+
export default () => kitten.html`
|
|
25
|
+
<h1>Hello, world! <${kitten.icons.Smiley} /></h1>
|
|
26
|
+
`
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
> 💡 Put the above code in a file called _index.page.js_ and run `kitten` from the same directory if you want to follow along.
|
|
30
|
+
|
|
31
|
+
While it works, you won’t get language intelligence for the `kitten` global, so you won’t be able to, for example, see all the other possible `kitten.icons` you could be using.
|
|
32
|
+
|
|
33
|
+
How you add language intelligence and static type checking for Kitten to your project differs based on whether you’re using JavaScript or [TypeScript](https://www.typescriptlang.org/).
|
|
34
|
+
|
|
35
|
+
### JavaScript
|
|
36
|
+
|
|
37
|
+
1. [Install Kitten Types (@small-web/kitten-types)](#install) as a development-time dependency.
|
|
38
|
+
2. *Either* add `@ts-check` to the tops of your JavaScript files or add a jsconfig.json file to your project to enable type checking.
|
|
39
|
+
|
|
40
|
+
That’s it!
|
|
41
|
+
|
|
42
|
+
In a JavaScript project, the global types will automatically be picked up by the TypeScript language server.
|
|
43
|
+
|
|
44
|
+
So your example becomes the following with full type safety and intelligence:
|
|
45
|
+
|
|
46
|
+
```js
|
|
47
|
+
// @ts-check
|
|
48
|
+
export default () => kitten.html`
|
|
49
|
+
<h1>Hello, world! <${kitten.icons.Smiley} /></h1>
|
|
50
|
+
`
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
To add type annotations to your project, you can use [JSDoc](https://jsdoc.app/).
|
|
54
|
+
|
|
55
|
+
### TypeScript
|
|
56
|
+
|
|
57
|
+
There’s one extra step (compared to JavaScript) when using TypeScript.
|
|
58
|
+
|
|
59
|
+
1. [Install Kitten Types (@small-web/kitten-types)](#install) as a development-time dependency.
|
|
60
|
+
|
|
61
|
+
2. Create a tsconfig.json file.
|
|
62
|
+
|
|
63
|
+
3. Create a *globals.d.ts* file in your project:
|
|
64
|
+
|
|
65
|
+
```ts
|
|
66
|
+
import '@small-web/kitten-types'
|
|
67
|
+
export {}
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
That’s it!
|
|
72
|
+
|
|
73
|
+
The global `kitten` type information will now be picked up by your editor.
|
|
74
|
+
|
|
75
|
+
### Importing types for explicit annotations
|
|
76
|
+
|
|
77
|
+
When you need to annotate function parameters, etc., import the types directly.
|
|
78
|
+
|
|
79
|
+
In TypeScript:
|
|
80
|
+
|
|
81
|
+
```ts
|
|
82
|
+
import type { KittenRequest, KittenResponse } from '@small-web/kitten-types'
|
|
83
|
+
|
|
84
|
+
export default ({ request, response }: { request: KittenRequest, response: KittenResponse }) =>
|
|
85
|
+
kitten.html`<h1>Kitten</h1>`
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
And, in JavaScript:
|
|
89
|
+
|
|
90
|
+
```js
|
|
91
|
+
// @ts-check
|
|
92
|
+
/**
|
|
93
|
+
@param {{
|
|
94
|
+
request: import('@small-web/kitten-types').KittenRequest,
|
|
95
|
+
response: import('@small-web/kitten-types').KittenResponse
|
|
96
|
+
}} parameters
|
|
97
|
+
*/
|
|
98
|
+
export default ({ request, response }) => kitten.html`
|
|
99
|
+
<h1>Kitten</h1>
|
|
100
|
+
`
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
> 🔗 For more help in using this module, please see the [Kitten Type Safety Tutorial](https://kitten.small-web.org/tutorials/type-safety/).
|
|
104
|
+
|
|
105
|
+
## Database type safety
|
|
106
|
+
|
|
107
|
+
Kitten by default has at least two [databases](https://kitten.small-web.org/reference/#database) per project:
|
|
108
|
+
|
|
109
|
+
An internal database called `_db` and a custom one for you to use in your project called `db`.
|
|
110
|
+
|
|
111
|
+
This package provides a type-safe `_db` export for the internal database as the structure is well known.
|
|
112
|
+
|
|
113
|
+
If you want type safety for your custom project database, please create and use a type-safe database app module.
|
|
114
|
+
|
|
115
|
+
> 🔗 For a more in-depth look into database type safety, please see the [Database App Modules Kitten Tutorial](https://kitten.small-web.org/tutorials/database-app-modules/).
|
|
116
|
+
|
|
117
|
+
## Relationship to `@small-web/kitten`
|
|
118
|
+
|
|
119
|
+
The older [`@small-web/kitten`](https://www.npmjs.com/package/@small-web/kitten) globals module cast the members of `globalThis.kitten` at runtime and provided type safety for JavaScript projects (prior to TypeScript support being added to Kitten).
|
|
120
|
+
|
|
121
|
+
This was problematic as a discrepency between Kitten and the globals module was not just a missing completion in your editor but could break your app at runtime.
|
|
122
|
+
|
|
123
|
+
The `@small-web/kitten` package is now deprecated but kept available for backwards compatibility. It also now sources all of its types from this package (`@small-web/kitten-types`).
|
|
124
|
+
|
|
125
|
+
New projects should use this types-only package and existing projects should be migrated over.
|
|
126
|
+
|
|
127
|
+
## Like this? Fund us!
|
|
128
|
+
|
|
129
|
+
[Small Technology Foundation](https://small-tech.org) is a tiny, independent not-for-profit.
|
|
130
|
+
|
|
131
|
+
We exist in part thanks to patronage by people like you. If you share [our vision](https://small-tech.org/about/#small-technology) and want to support our work, please [become a patron or donate to us](https://small-tech.org/fund-us) today and help us continue to exist.
|
|
132
|
+
|
|
133
|
+
## License
|
|
134
|
+
|
|
135
|
+
Copyright © 2026-present [Aral Balkan](https://ar.al), [Small Technology Foundation](https://small-tech.org)
|
|
136
|
+
Released under [AGPL 3.0](https://www.gnu.org/licenses/agpl-3.0.en.html).
|
package/global.d.ts
ADDED
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
// @small-web/kitten-types — ambient declaration of the global `kitten` namespace.
|
|
2
|
+
//
|
|
3
|
+
// This file declares the shape of `globalThis.kitten` so that consumers can use
|
|
4
|
+
// the `kitten` global directly with full type safety, without importing a
|
|
5
|
+
// runtime shim purely for types.
|
|
6
|
+
//
|
|
7
|
+
// The `KittenGlobal` interface is the single, hand-written source of truth for
|
|
8
|
+
// the global namespace shape (previously reconstructed from runtime casts in
|
|
9
|
+
// `@small-web/kitten`’s `globals.js`).
|
|
10
|
+
|
|
11
|
+
import type { EventEmitter } from 'node:events'
|
|
12
|
+
import type { Server } from 'node:https'
|
|
13
|
+
|
|
14
|
+
import type {
|
|
15
|
+
Session,
|
|
16
|
+
Upload,
|
|
17
|
+
KittenComponent,
|
|
18
|
+
KittenPage,
|
|
19
|
+
MarkdownIt,
|
|
20
|
+
Polka,
|
|
21
|
+
WebSocket as KittenWebSocket,
|
|
22
|
+
slugify as Slugify,
|
|
23
|
+
yaml as Yaml,
|
|
24
|
+
Point,
|
|
25
|
+
Signature
|
|
26
|
+
} from './types.d.ts'
|
|
27
|
+
|
|
28
|
+
import type { KittenIcons } from 'kitten-icons/types.d.ts'
|
|
29
|
+
|
|
30
|
+
/** Tagged template function (e.g. `kitten.html`, `kitten.css`). */
|
|
31
|
+
export type TaggedTemplate = (
|
|
32
|
+
strings: TemplateStringsArray,
|
|
33
|
+
...properties: any[]
|
|
34
|
+
) => string | Array<string> | Promise<string | Array<string>>
|
|
35
|
+
|
|
36
|
+
/** Sanitisation helper (e.g. `kitten.safelyAddHtml`, `kitten.sanitise`). */
|
|
37
|
+
export type SanitisationFunction = (
|
|
38
|
+
untrustedContent: string,
|
|
39
|
+
allowedTags?: Array<string>,
|
|
40
|
+
contact?: boolean
|
|
41
|
+
) => string
|
|
42
|
+
|
|
43
|
+
type StatsObject = Record<string, number>
|
|
44
|
+
|
|
45
|
+
/** Internal Kitten database (`kitten._db`). */
|
|
46
|
+
export interface KittenInternalDb {
|
|
47
|
+
sessions: Record<string, Session>
|
|
48
|
+
settings: {
|
|
49
|
+
autoUpdate: {
|
|
50
|
+
interval: number
|
|
51
|
+
}
|
|
52
|
+
domainToken: string
|
|
53
|
+
evergreenWebUrl: string
|
|
54
|
+
hideWelcomeMessage: boolean
|
|
55
|
+
id: {
|
|
56
|
+
ed25519: {
|
|
57
|
+
asString: string
|
|
58
|
+
}
|
|
59
|
+
ssh: {
|
|
60
|
+
asString: string
|
|
61
|
+
fingerprint: string
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
packageLockFileHashes: Record<string, string>
|
|
65
|
+
path: string
|
|
66
|
+
smallWebHostDomain: string
|
|
67
|
+
stats: {
|
|
68
|
+
hits: StatsObject
|
|
69
|
+
pages: StatsObject
|
|
70
|
+
missing: StatsObject
|
|
71
|
+
referrers: StatsObject
|
|
72
|
+
serverErrors: StatsObject
|
|
73
|
+
}
|
|
74
|
+
uploads: UploadStore
|
|
75
|
+
webhookSecret: string
|
|
76
|
+
}
|
|
77
|
+
uploads: Record<string, Upload>
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** The `kitten.uploads` store. */
|
|
81
|
+
export type UploadStore = {
|
|
82
|
+
get(id: string): Upload
|
|
83
|
+
length(): number
|
|
84
|
+
all(): Upload[]
|
|
85
|
+
allIds(): string[]
|
|
86
|
+
delete(id: string): Promise<void>
|
|
87
|
+
} & Record<string, Upload>
|
|
88
|
+
|
|
89
|
+
interface ServerOptions {
|
|
90
|
+
domain?: string
|
|
91
|
+
port?: number
|
|
92
|
+
aliases?: string
|
|
93
|
+
open?: boolean
|
|
94
|
+
'working-directory'?: string
|
|
95
|
+
'domain-token'?: string
|
|
96
|
+
'small-web-host-domain'?: string
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
The shape of the global `kitten` namespace.
|
|
101
|
+
*/
|
|
102
|
+
export interface KittenGlobal {
|
|
103
|
+
version: {
|
|
104
|
+
date: Date
|
|
105
|
+
versionStamp: number
|
|
106
|
+
gitHash: string
|
|
107
|
+
apiVersion: number
|
|
108
|
+
nodeVersion: string
|
|
109
|
+
exactVersion: string
|
|
110
|
+
birthday: string
|
|
111
|
+
starSign: string
|
|
112
|
+
Component: () => Function
|
|
113
|
+
html: () => string
|
|
114
|
+
printToConsole: () => void
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
app: {
|
|
118
|
+
package?: any
|
|
119
|
+
router: Polka
|
|
120
|
+
server: Server
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// db is the custom database. If you want type safety for it, create a
|
|
124
|
+
// database app module in your project and declare the types there.
|
|
125
|
+
// See: https://codeberg.org/kitten/app#database-app-module
|
|
126
|
+
db: any
|
|
127
|
+
|
|
128
|
+
_db: KittenInternalDb
|
|
129
|
+
|
|
130
|
+
Upload: typeof Upload
|
|
131
|
+
uploads: UploadStore
|
|
132
|
+
icons: KittenIcons
|
|
133
|
+
|
|
134
|
+
domain: string
|
|
135
|
+
port: number
|
|
136
|
+
|
|
137
|
+
databaseDirectory: string
|
|
138
|
+
projectIdentifier: string
|
|
139
|
+
|
|
140
|
+
appRepository: {
|
|
141
|
+
upgradeAppToLatestCompatibleVersion(): Promise<void>
|
|
142
|
+
upgradeAppToLatestAvailableCommit(): Promise<void>
|
|
143
|
+
updateAppToVersion(versionTag: string): Promise<void>
|
|
144
|
+
update(): Promise<void>
|
|
145
|
+
hasCompatibleAppVersion: boolean
|
|
146
|
+
canBeUpdated: boolean
|
|
147
|
+
hasNewerApiVersion: boolean
|
|
148
|
+
upgradeOrDowngrade(versionTag: string, lowercase: boolean): 'upgrade' | 'downgrade'
|
|
149
|
+
isEqualToVersion(versionTag: string): boolean
|
|
150
|
+
UpdateButtonComponent(): (type: string, version: string, small: boolean) => string | Array<string>
|
|
151
|
+
VersionComponent(): (version: string, compatible: boolean, brief: boolean) => string | Array<string>
|
|
152
|
+
CurrentAppVersionComponent(): () => string | Array<string>
|
|
153
|
+
AllAvailableVersionsComponent(): () => string | Array<string>
|
|
154
|
+
displayAppVersion(): void
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
automaticUpdates: {
|
|
158
|
+
interval: number
|
|
159
|
+
intervalInHours: number
|
|
160
|
+
timeToNextCheck(): number
|
|
161
|
+
timeToNextCheckPretty(): string
|
|
162
|
+
start(): void
|
|
163
|
+
checkForUpdates(): Promise<void>
|
|
164
|
+
stop(): void
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// Note the mapping: `package` is exposed here even though the runtime shim
|
|
168
|
+
// exports it as `_package` (because `package` is a reserved word in strict
|
|
169
|
+
// mode for value bindings). As an interface property name it is valid.
|
|
170
|
+
package: {
|
|
171
|
+
hasCompatibleVersion: boolean
|
|
172
|
+
isLatestReleaseVersion: boolean
|
|
173
|
+
isMoreRecentThanReleaseVersion: boolean
|
|
174
|
+
canBeUpgraded: boolean
|
|
175
|
+
upgrade(apiVersion: number | undefined): void
|
|
176
|
+
update(): Promise<void>
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
requests: Array<string>
|
|
180
|
+
pages: Record<string, KittenPage>
|
|
181
|
+
events: EventEmitter
|
|
182
|
+
|
|
183
|
+
Component: typeof KittenComponent
|
|
184
|
+
Page: typeof KittenPage
|
|
185
|
+
|
|
186
|
+
html: TaggedTemplate
|
|
187
|
+
css: TaggedTemplate
|
|
188
|
+
js: TaggedTemplate
|
|
189
|
+
markdown: TaggedTemplate
|
|
190
|
+
md: MarkdownIt
|
|
191
|
+
|
|
192
|
+
slugify: typeof Slugify
|
|
193
|
+
yaml: typeof Yaml
|
|
194
|
+
|
|
195
|
+
crypto: {
|
|
196
|
+
bytesToHex: (uint8a: Uint8Array) => string
|
|
197
|
+
decrypt: (sharedKey: Uint8Array, encoded: string | Uint8Array) => Uint8Array
|
|
198
|
+
emojiStringToSecret: (emojiString: string) => Uint8Array
|
|
199
|
+
encrypt: (sharedKey: Uint8Array, plaintext: string | Uint8Array) => Uint8Array
|
|
200
|
+
getSharedSecret: (
|
|
201
|
+
privateKey: Uint8Array | string | bigint | number,
|
|
202
|
+
publicKey: Uint8Array | string
|
|
203
|
+
) => Promise<Uint8Array>
|
|
204
|
+
hexToBytes: (hex: string) => Uint8Array
|
|
205
|
+
randomBytes: (bytesLength?: number) => Uint8Array
|
|
206
|
+
random32ByteTokenInHex: () => string
|
|
207
|
+
secretToEmojiString: (secret: Uint8Array) => string
|
|
208
|
+
sign: (message: Uint8Array | string, privateKey: Uint8Array | string) => Promise<Uint8Array>
|
|
209
|
+
verify: (
|
|
210
|
+
sig: Uint8Array | string | Signature,
|
|
211
|
+
message: Uint8Array | string,
|
|
212
|
+
publicKey: Uint8Array | string | Point
|
|
213
|
+
) => Promise<boolean>
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
utils: {
|
|
217
|
+
ALL_ROUTE_EXTENSIONS: Array<string>
|
|
218
|
+
BACKEND_EXTENSIONS: Array<string>
|
|
219
|
+
DEPENDENCY_EXTENSIONS: Array<string>
|
|
220
|
+
DYNAMIC_ROUTE_EXTENSIONS: Array<string>
|
|
221
|
+
FRONTEND_EXTENSIONS: Array<string>
|
|
222
|
+
HTTP_METHODS: Array<string>
|
|
223
|
+
STATIC_ROUTE_EXTENSIONS: Array<string>
|
|
224
|
+
classNameFromFilePath: (filePath: string, basePath: string) => string
|
|
225
|
+
classNameFromRoutePattern: (pattern: string) => string
|
|
226
|
+
db: {
|
|
227
|
+
keypathForChange(change: string): string
|
|
228
|
+
}
|
|
229
|
+
decodeFilePath: (filePath: string) => string
|
|
230
|
+
encodeFilePath: (filePath: string) => string
|
|
231
|
+
exitWithError: (message: string) => void
|
|
232
|
+
extensionCategories: {
|
|
233
|
+
backendRoutes: Array<string>
|
|
234
|
+
frontendRoutes: Array<string>
|
|
235
|
+
dependencies: Array<string>
|
|
236
|
+
dynamicRoutes: Array<string>
|
|
237
|
+
staticRoutes: Array<string>
|
|
238
|
+
allRoutes: Array<string>
|
|
239
|
+
}
|
|
240
|
+
extensionOfFilePath: (filePath: string) => string
|
|
241
|
+
getDomainsAndPort: (options: ServerOptions) => { domains: string[]; port: number }
|
|
242
|
+
getProjectIdentifierForDomain: (domain: string, port: number) => string
|
|
243
|
+
kittenAppPath: string
|
|
244
|
+
npm: (command: 'ci' | 'install', modulePath: string) => boolean
|
|
245
|
+
routePatternFromFilePath: (filePath: string, basePath: string) => string
|
|
246
|
+
runNpmCiOnModulePath: (modulePath: string) => void
|
|
247
|
+
setBasePath: (workingDirectory: string, pathToServe: string) => string
|
|
248
|
+
supportedExtensionsRegExp: RegExp
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
libraries: {
|
|
252
|
+
htmx: string
|
|
253
|
+
htmxIdiomorph: string
|
|
254
|
+
htmxWebSocket: string
|
|
255
|
+
alpineJs: string
|
|
256
|
+
water: string
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
page: {
|
|
260
|
+
html: 'HTML'
|
|
261
|
+
head: 'HEAD'
|
|
262
|
+
startOfBody: 'START_OF_BODY'
|
|
263
|
+
beforeLibraries: 'BEFORE_LIBRARIES'
|
|
264
|
+
afterLibraries: 'AFTER_LIBRARIES'
|
|
265
|
+
endOfBody: 'END_OF_BODY'
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
safelyAddHtml: SanitisationFunction
|
|
269
|
+
sanitise: SanitisationFunction
|
|
270
|
+
|
|
271
|
+
WebSocket: typeof KittenWebSocket
|
|
272
|
+
|
|
273
|
+
deprecationWarnings: Array<string>
|
|
274
|
+
|
|
275
|
+
paths: {
|
|
276
|
+
APP_DATA_DIRECTORY: string
|
|
277
|
+
APP_UPLOADS_DIRECTORY: string
|
|
278
|
+
APP_REPL_DIRECTORY: string
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
deploy: Function
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
declare global {
|
|
285
|
+
var kitten: KittenGlobal
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
export {}
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
// @small-web/kitten-types
|
|
2
|
+
//
|
|
3
|
+
// Types-only package for Kitten (https://kitten.small-web.org).
|
|
4
|
+
//
|
|
5
|
+
// This entry point re-exports the reusable Kitten types (for explicit
|
|
6
|
+
// annotations) and pulls in the ambient `declare global` declaration of the
|
|
7
|
+
// `kitten` namespace (so that the global `kitten` object is typed wherever
|
|
8
|
+
// this package is on the type-checker’s include path).
|
|
9
|
+
|
|
10
|
+
// Side-effect import: brings the ambient `declare global { var kitten }` into
|
|
11
|
+
// scope for any project that depends on this package.
|
|
12
|
+
import './global.d.ts'
|
|
13
|
+
|
|
14
|
+
// Reusable types, re-exported for explicit annotations
|
|
15
|
+
// (e.g. `import type { KittenRequest } from '@small-web/kitten-types'`).
|
|
16
|
+
export type {
|
|
17
|
+
Session,
|
|
18
|
+
WebSocket,
|
|
19
|
+
BufferLike,
|
|
20
|
+
slugify,
|
|
21
|
+
Polka,
|
|
22
|
+
MarkdownIt,
|
|
23
|
+
Upload,
|
|
24
|
+
LazilyLoadedRoute,
|
|
25
|
+
MessageSender,
|
|
26
|
+
Listener,
|
|
27
|
+
KittenComponent,
|
|
28
|
+
KittenPage,
|
|
29
|
+
ParsedURL,
|
|
30
|
+
PolkaResponse,
|
|
31
|
+
PolkaRequest,
|
|
32
|
+
KittenRequest,
|
|
33
|
+
KittenResponse,
|
|
34
|
+
KittenPostRequest,
|
|
35
|
+
Point,
|
|
36
|
+
Signature
|
|
37
|
+
} from './types.d.ts'
|
|
38
|
+
|
|
39
|
+
export { yaml } from './types.d.ts'
|
|
40
|
+
|
|
41
|
+
// Also export the hand-written global namespace shape, in case a consumer
|
|
42
|
+
// wants to reference it explicitly (e.g. `typeof kitten`).
|
|
43
|
+
export type { KittenGlobal } from './global.d.ts'
|
package/package.json
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@small-web/kitten-types",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Types-only package for Kitten: declares the global `kitten` namespace and exports Kitten types.",
|
|
5
|
+
"types": "index.d.ts",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"types": "./index.d.ts"
|
|
9
|
+
},
|
|
10
|
+
"./global.d.ts": "./global.d.ts",
|
|
11
|
+
"./types.d.ts": "./types.d.ts"
|
|
12
|
+
},
|
|
13
|
+
"type": "module",
|
|
14
|
+
"license": "AGPL-3.0",
|
|
15
|
+
"private": false,
|
|
16
|
+
"engines": {
|
|
17
|
+
"node": ">= 24",
|
|
18
|
+
"npm": ">= 8.0.0"
|
|
19
|
+
},
|
|
20
|
+
"homepage": "https://codeberg.org/kitten/types",
|
|
21
|
+
"repository": {
|
|
22
|
+
"type": "git",
|
|
23
|
+
"url": "https://codeberg.org/kitten/types"
|
|
24
|
+
},
|
|
25
|
+
"bugs": "https://codeberg.org/kitten/types/issues",
|
|
26
|
+
"funding": {
|
|
27
|
+
"type": "foundation",
|
|
28
|
+
"url": "https://small-tech.org/fund-us/"
|
|
29
|
+
},
|
|
30
|
+
"keywords": [
|
|
31
|
+
"Kitten",
|
|
32
|
+
"namespace",
|
|
33
|
+
"globals",
|
|
34
|
+
"type checking",
|
|
35
|
+
"static types",
|
|
36
|
+
"types",
|
|
37
|
+
"declare global",
|
|
38
|
+
"Small Web",
|
|
39
|
+
"Small Tech"
|
|
40
|
+
],
|
|
41
|
+
"author": {
|
|
42
|
+
"name": "Aral Balkan",
|
|
43
|
+
"email": "aral@small-tech.org",
|
|
44
|
+
"url": "https://ar.al"
|
|
45
|
+
},
|
|
46
|
+
"peerDependencies": {
|
|
47
|
+
"@types/node": ">=24"
|
|
48
|
+
},
|
|
49
|
+
"dependencies": {
|
|
50
|
+
"@types/markdown-it": "^14.1.2",
|
|
51
|
+
"@types/node": ">=24",
|
|
52
|
+
"kitten-icons": "^4.1.0"
|
|
53
|
+
}
|
|
54
|
+
}
|
package/polka/index.d.ts
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import type { IncomingMessage, ServerResponse } from 'http';
|
|
2
|
+
import type { ListenOptions, Server } from 'net';
|
|
3
|
+
|
|
4
|
+
// Parsed URL
|
|
5
|
+
|
|
6
|
+
declare interface ParsedURL {
|
|
7
|
+
pathname: string;
|
|
8
|
+
search: string;
|
|
9
|
+
query: Record<string, string | string[]> | void;
|
|
10
|
+
raw: string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
declare function parse(req: IncomingMessage): ParsedURL;
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
// Trouter
|
|
17
|
+
|
|
18
|
+
// Thank you: @fwilkerson, @stahlstift
|
|
19
|
+
// ---
|
|
20
|
+
|
|
21
|
+
/** @type {import('http').METHODS} */
|
|
22
|
+
type Methods = 'ACL' | 'BIND' | 'CHECKOUT' | 'CONNECT' | 'COPY' | 'DELETE' | 'GET' | 'HEAD' | 'LINK' | 'LOCK' |'M-SEARCH' | 'MERGE' | 'MKACTIVITY' |'MKCALENDAR' | 'MKCOL' | 'MOVE' |'NOTIFY' | 'OPTIONS' | 'PATCH' | 'POST' | 'PRI' | 'PROPFIND' | 'PROPPATCH' | 'PURGE' | 'PUT' | 'REBIND' | 'REPORT' | 'SEARCH' | 'SOURCE' | 'SUBSCRIBE' | 'TRACE' | 'UNBIND' | 'UNLINK' | 'UNLOCK' | 'UNSUBSCRIBE';
|
|
23
|
+
|
|
24
|
+
type Pattern = RegExp | string;
|
|
25
|
+
|
|
26
|
+
declare class Trouter<T = Function> {
|
|
27
|
+
find(method: Methods, url: string): {
|
|
28
|
+
params: Record<string, string>;
|
|
29
|
+
handlers: T[];
|
|
30
|
+
};
|
|
31
|
+
add(method: Methods, pattern: Pattern, ...handlers: T[]): this;
|
|
32
|
+
use(pattern: Pattern, ...handlers: T[]): this;
|
|
33
|
+
all(pattern: Pattern, ...handlers: T[]): this;
|
|
34
|
+
get(pattern: Pattern, ...handlers: T[]): this;
|
|
35
|
+
head(pattern: Pattern, ...handlers: T[]): this;
|
|
36
|
+
patch(pattern: Pattern, ...handlers: T[]): this;
|
|
37
|
+
options(pattern: Pattern, ...handlers: T[]): this;
|
|
38
|
+
connect(pattern: Pattern, ...handlers: T[]): this;
|
|
39
|
+
delete(pattern: Pattern, ...handlers: T[]): this;
|
|
40
|
+
trace(pattern: Pattern, ...handlers: T[]): this;
|
|
41
|
+
post(pattern: Pattern, ...handlers: T[]): this;
|
|
42
|
+
put(pattern: Pattern, ...handlers: T[]): this;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// Polka
|
|
46
|
+
|
|
47
|
+
type Promisable<T> = Promise<T> | T;
|
|
48
|
+
type ListenCallback = () => Promisable<void>;
|
|
49
|
+
|
|
50
|
+
declare namespace polka {
|
|
51
|
+
export interface IError extends Error {
|
|
52
|
+
code?: number;
|
|
53
|
+
status?: number;
|
|
54
|
+
details?: any;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export type NextHandler = (err?: string | IError) => Promisable<void>;
|
|
58
|
+
export type ErrorHandler<T extends Request = Request> = (err: string | IError, req: T, res: Response, next: NextHandler) => Promisable<void>;
|
|
59
|
+
export type Middleware<T extends IncomingMessage = Request> = (req: T & Request, res: Response, next: NextHandler) => Promisable<void>;
|
|
60
|
+
|
|
61
|
+
export interface IOptions<T extends Request = Request> {
|
|
62
|
+
server?: Server;
|
|
63
|
+
onNoMatch?: Middleware<T>;
|
|
64
|
+
onError?: ErrorHandler<T>;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export type Response = ServerResponse;
|
|
68
|
+
|
|
69
|
+
export interface Request extends IncomingMessage {
|
|
70
|
+
url: string;
|
|
71
|
+
method: string;
|
|
72
|
+
originalUrl: string;
|
|
73
|
+
params: Record<string, string>;
|
|
74
|
+
path: string;
|
|
75
|
+
search: string;
|
|
76
|
+
query: Record<string,string>;
|
|
77
|
+
body?: any;
|
|
78
|
+
_decoded?: true;
|
|
79
|
+
_parsedUrl: ParsedURL;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export interface Polka<T extends Request = Request> extends Trouter<Middleware<T>> {
|
|
83
|
+
readonly server: Server;
|
|
84
|
+
readonly wares: Middleware<T>[];
|
|
85
|
+
|
|
86
|
+
readonly onError: ErrorHandler<T>;
|
|
87
|
+
readonly onNoMatch: Middleware<T>;
|
|
88
|
+
|
|
89
|
+
readonly handler: Middleware<T>;
|
|
90
|
+
parse: (req: IncomingMessage) => ParsedURL;
|
|
91
|
+
|
|
92
|
+
use(pattern: RegExp|string, ...handlers: (Polka<T> | Middleware<T>)[]): this;
|
|
93
|
+
use(...handlers: (Polka<T> | Middleware<T>)[]): this;
|
|
94
|
+
|
|
95
|
+
listen(port?: number, hostname?: string, backlog?: number, callback?: ListenCallback): this;
|
|
96
|
+
listen(port?: number, hostname?: string, callback?: ListenCallback): this;
|
|
97
|
+
listen(port?: number, backlog?: number, callback?: ListenCallback): this;
|
|
98
|
+
listen(port?: number, callback?: ListenCallback): this;
|
|
99
|
+
listen(path: string, backlog?: number, callback?: ListenCallback): this;
|
|
100
|
+
listen(path: string, callback?: ListenCallback): this;
|
|
101
|
+
listen(options: ListenOptions, callback?: ListenCallback): this;
|
|
102
|
+
listen(handle: any, backlog?: number, callback?: ListenCallback): this;
|
|
103
|
+
listen(handle: any, callback?: ListenCallback): this;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
declare function polka<T extends polka.Request = polka.Request>(
|
|
108
|
+
options?: polka.IOptions<T>
|
|
109
|
+
): polka.Polka<T>;
|
|
110
|
+
|
|
111
|
+
export default polka;
|