Skip to content

Run dedicated typescript types tests using tsd #363

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

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
20 changes: 19 additions & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,29 @@ jobs:
cache: npm
- run: npm clean-install
- run: npm run lint
types:
name: Typescript types check
needs: [lint]
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
persist-credentials: false
show-progress: false

- name: setup node
uses: actions/setup-node@v4
with:
node-version: 20
cache: npm
- run: npm clean-install
- run: npm run test:types

unittest:
name: unit tests
runs-on: ubuntu-latest
needs: [lint]
needs: [types]
strategy:
matrix:
node: [16, 18, 20, 22]
Expand Down
7 changes: 1 addition & 6 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,7 @@ declare namespace OAuth2Server {
* Instantiates Request using the supplied options.
*
*/
constructor(options: {
headers: Record<string, string>,
method: string,
query: Record<string, string>,
body?: any
} & Record<string, any> | http.IncomingMessage);
constructor(options?: Record<string, any> | http.IncomingMessage);

/**
* Returns the specified HTTP header field. The match is case-insensitive.
Expand Down
143 changes: 143 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
import {expectType} from 'tsd';
import express from 'express'
import {
OAuth2Server,
OAuthError,
Request,
Response,
AccessDeniedError,
InsufficientScopeError,
InvalidArgumentError,
InvalidClientError,
InvalidGrantError,
InvalidRequestError,
InvalidScopeError,
InvalidTokenError,
ServerError,
UnauthorizedClientError,
UnauthorizedRequestError,
UnsupportedGrantTypeError,
UnsupportedResponseTypeError
} from '.';
import {Request as UndiciRequest, Response as UndiciResponse} from 'undici';

// ----------------------------------------------------------------------------
// REQUEST
// ----------------------------------------------------------------------------
function testRequest(req: Request): void {
expectType<string>(req.method);
expectType<Record<string, string>>(req.headers);
expectType<Record<string, string>>(req.query);
expectType<any>(req.body);
}

const args = [
undefined,
{},
{method: 'get'},
{method: 'get', headers: {x: 'y'}},
{method: 'get', query: {moo: 'foo'}},
{method: 'get', headers: {x: 'y'}, query: {moo: 'foo'}},
{method: 'get', headers: {x: 'y'}, query: {moo: 'foo', bar: 'baz'}},
// check for express request compatibility
new express.Request({
method: 'get',
query: {moo: 'foo'},
headers: {x: 'y'}
}),
// check for compatibility with fetch api Request
// we use undici Request as a stand-in for fetch api Request
// because we still support older versions of Node.js
new UndiciRequest(
'https://example.com?moo=foo%2Cbar',
{
method: 'get', headers: {x: 'y'}
})
];

for (const arg of args) {
testRequest(new Request(arg));
}


// ----------------------------------------------------------------------------
// RESPONSE
// ----------------------------------------------------------------------------
function testResponse(res: Response): void {
expectType<number>(res.status);
expectType<Record<string, string>>(res.headers);
expectType<any>(res.body);
}

const resArgs = [
undefined,
{},
{status: 200},
{status: 200, headers: {x: 'y'}},
{status: 200, body: 'foo'},
{status: 200, headers: {x: 'y'}, body: 'foo'},
{status: 200, headers: {x: 'y'}, body: 'foo', statusText: 'OK'},
// check for express response compatibility
new express.Response({
status: 200,
headers: {x: 'y'},
body: 'foo'
}),
// check for compatibility with fetch api Response
// we use undici Response as a stand-in for fetch api Response
// because we still support older versions of Node.js
new UndiciResponse(
'foo',
{
status: 200,
headers: {x: 'y'},
statusText: 'OK'
})
];

for (const arg of resArgs) {
testResponse(new Response(arg));
}

// ----------------------------------------------------------------------------
// ERRORS
// ----------------------------------------------------------------------------
function testError(err: OAuthError): void {
expectType<string>(err.name);
expectType<string>(err.message);
expectType<number>(err.code);
}

const errorTypes = [
AccessDeniedError,
InsufficientScopeError,
InvalidArgumentError,
InvalidClientError,
InvalidGrantError,
InvalidRequestError,
InvalidScopeError,
InvalidTokenError,
ServerError,
UnauthorizedClientError,
UnauthorizedRequestError,
UnsupportedGrantTypeError,
UnsupportedResponseTypeError
];

const errorArgs = [
undefined,
{},
{message: 'foo'},
{message: 'foo', code: 400},
{message: 'foo', code: 400, data: {bar: 'baz'}},
// check for express error compatibility
new express.Error('foo'),
// native error compatibility
new Error('foo')
];

for (const arg of errorArgs) {
for (const ErrorType of errorTypes) {
testError(new ErrorType(arg));
}
}
Loading