Skip to content
Merged
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
97 changes: 97 additions & 0 deletions lib/core/lib/core/headers.tsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import "@typespec/http";
import "@typespec/versioning";
import "./types.tsp";

/** Version negotiation headers (ADR 0032), added in protocol version 0.6.0
*
* @example How to add the version request header to a declared protocol route
*
* ```typespec
* import "@typespec/http";
* import "@common-grants/core";
*
* using CommonGrants; // exposes the Headers namespace
* using TypeSpec.Http;
* using TypeSpec.Versioning;
*
* @service
* @useDependency(CommonGrants.Versions.v0_6)
* namespace MyApi;
*
* @route("/common-grants/opportunities")
* @get
* op listOpportunities(
* ...Pagination.PaginatedQueryParams,
* ...Headers.VersionRequest,
* ): Responses.PaginatedT<Models.OpportunityBase>;
* ```
*/
namespace CommonGrants.Headers;

/** Request headers a client sends to select a protocol version */
@Versioning.added(CommonGrants.Versions.v0_6)
model VersionRequest {
/** The protocol version the client wants, such as `0.6.0`
*
* Servers match on major and minor version. When absent, the server responds
* in its default version.
*/
@Http.header("CG-Version")
// Spreading drops model-level `@added`, so each property repeats it
@Versioning.added(CommonGrants.Versions.v0_6)
cgVersion?: Types.protocolVersion;
}

/** Response headers that report the protocol version a response was served in */
@Versioning.added(CommonGrants.Versions.v0_6)
model VersionResponse {
/** The full protocol version this response was served in, such as `0.5.1`
*
* Can differ from the requested version in the patch number only.
*/
@Http.header("CG-Version-Served")
@Versioning.added(CommonGrants.Versions.v0_6)
cgVersionServed: Types.protocolVersion;

...VersionLifecycle;
}

/** Version headers for 401 and 403 responses */
@Versioning.added(CommonGrants.Versions.v0_6)
model VersionResponseAuth {
/** The full protocol version this response was served in, such as `0.5.1`
*
* Optional because authentication can run before a version is selected.
*/
@Http.header("CG-Version-Served")
@Versioning.added(CommonGrants.Versions.v0_6)
cgVersionServed?: Types.protocolVersion;

...VersionLifecycle;
}

/** Caching and retirement headers shared by `VersionResponse` and `VersionResponseAuth` */
@Versioning.added(CommonGrants.Versions.v0_6)
model VersionLifecycle {
/** Request headers that caches must key this response on, such as `CG-Version` */
@Http.header("Vary")
@Versioning.added(CommonGrants.Versions.v0_6)
vary?: string;

/** When the served version was or will be deprecated (RFC 9745), such as `@1796083200` */
@pattern("^@-?[0-9]+$")
@Http.header("Deprecation")
@Versioning.added(CommonGrants.Versions.v0_6)
deprecation?: string;

/** When the served version stops responding (RFC 8594), such as `Tue, 01 Jun 2027 00:00:00 GMT` */
@encode(DateTimeKnownEncoding.rfc7231)
@Http.header("Sunset")
@Versioning.added(CommonGrants.Versions.v0_6)
sunset?: utcDateTime;

/** Links about the served version's lifecycle (RFC 8288), such as `rel="sunset"` */
@Http.header("Link")
@Versioning.added(CommonGrants.Versions.v0_6)
link?: string;
}
1 change: 1 addition & 0 deletions lib/core/lib/core/index.tsp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import "./types.tsp";
import "./headers.tsp";
import "./fields/index.tsp";
import "./models/index.tsp";
import "./routes/index.tsp";
Expand Down
14 changes: 11 additions & 3 deletions lib/core/lib/core/responses/error.tsp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import "../headers.tsp";
import "@typespec/http";

namespace CommonGrants.Responses;
Expand All @@ -10,6 +11,11 @@ namespace CommonGrants.Responses;
* import "@typespec/http"
*
* alias Unauthorized = Error & Http.UnauthorizedResponse
*
* // From protocol version 0.6.0, also carry the version response headers
* alias VersionedUnauthorized = Error &
* Http.UnauthorizedResponse &
* Headers.VersionResponseAuth
* ```
*/
@error
Expand All @@ -27,9 +33,11 @@ model Error {
errors: Array<unknown>;
}

alias Unauthorized = Error & Http.UnauthorizedResponse;
alias Forbidden = Error & Http.ForbiddenResponse;
alias NotFound = Error & Http.NotFoundResponse;
alias Unauthorized = Error &
Http.UnauthorizedResponse &
Headers.VersionResponseAuth;
alias Forbidden = Error & Http.ForbiddenResponse & Headers.VersionResponseAuth;
alias NotFound = Error & Http.NotFoundResponse & Headers.VersionResponse;

@example(#{
status: 400,
Expand Down
10 changes: 10 additions & 0 deletions lib/core/lib/core/responses/success.tsp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import "../headers.tsp";
import "../pagination.tsp";
import "../sorting.tsp";
import "@typespec/http";
Expand Down Expand Up @@ -57,6 +58,8 @@ model OkT<T> extends Success {
// Inherit the 200 status code
...Http.OkResponse;

...Headers.VersionResponse;

/** Response data */
data: T;
}
Expand Down Expand Up @@ -111,6 +114,7 @@ model PaginatedT<T> extends Success {
// Inherit the 200 status code
...Http.OkResponse;

...Headers.VersionResponse;
...PaginatedBase<T>;
}

Expand Down Expand Up @@ -162,6 +166,7 @@ model SortedT<T> extends Success {
// Inherit the 200 status code
...Http.OkResponse;

...Headers.VersionResponse;
...SortedBase<T>;
}

Expand Down Expand Up @@ -225,6 +230,7 @@ model FilteredT<ItemsT, FilterT> extends Success {
// Inherit the 200 status code
...Http.OkResponse;

...Headers.VersionResponse;
...FilteredBase<ItemsT, FilterT>;
}

Expand Down Expand Up @@ -253,6 +259,8 @@ model CreatedT<T> extends Success {
// Inherit the 201 status code
...Http.CreatedResponse;

...Headers.VersionResponse;

/** Response data */
data: T;
}
Expand Down Expand Up @@ -286,6 +294,8 @@ model AcceptedT<T> extends Success {
// Inherit the 202 status code
...Http.AcceptedResponse;

...Headers.VersionResponse;

/** URL where the accepted resource can be retrieved */
@Http.header("Location")
location?: url;
Expand Down
19 changes: 18 additions & 1 deletion lib/core/lib/core/routes/applications.tsp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ interface Applications {
/** The title of the application */
@Versioning.renamedFrom(Versions.v0_4, "name")
title: string,

...Headers.VersionRequest,
): Responses.CreatedT<Models.ApplicationBase> | Responses.Unauthorized;

// ###############################
Expand All @@ -46,6 +48,8 @@ interface Applications {
getApplication(
/** The ID of the application to get */
@path appId: Types.uuid,

...Headers.VersionRequest,
):
| Responses.OkT<Models.ApplicationBase>
| Responses.NotFound
Expand All @@ -62,9 +66,11 @@ interface Applications {
submitApplication(
/** The ID of the application to submit */
@path appId: Types.uuid,

...Headers.VersionRequest,
):
| Responses.OkT<unknown>
| Responses.ApplicationSubmissionError
| ApplicationSubmissionErrorResponse
| Responses.NotFound
| Responses.Unauthorized;

Expand All @@ -82,5 +88,16 @@ interface Applications {

/** The sorting to apply to the search */
@query sorting: Models.AppSorting,

...Headers.VersionRequest,
): Responses.FilteredT<Models.ApplicationBase, Models.AppFilters>;
}

// Lives here, not in `Responses`, so the headers stay out of the JSON Schemas
@error
model ApplicationSubmissionErrorResponse {
...Headers.VersionResponse;

/** The submission error */
@body body: Responses.ApplicationSubmissionError;
}
5 changes: 5 additions & 0 deletions lib/core/lib/core/routes/awards.tsp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ interface Awards {
@list
list<T extends Models.AwardBase = Models.AwardBase>(
...Pagination.PaginatedQueryParams,
...Headers.VersionRequest,
): Responses.PaginatedT<T>;

// ##############################
Expand All @@ -69,6 +70,8 @@ interface Awards {
/** The ID of the award to view */
@path
awdId: Types.uuid,

...Headers.VersionRequest,
): Responses.OkT<T> | Responses.NotFound;

// ###############################
Expand Down Expand Up @@ -101,5 +104,7 @@ interface Awards {

/** Pagination instructions for the results */
pagination?: Pagination.PaginatedBodyParams,

...Headers.VersionRequest,
): Responses.FilteredT<T, Models.AwdFilters>;
}
4 changes: 4 additions & 0 deletions lib/core/lib/core/routes/competitions.tsp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ interface Competitions {
read(
/** The ID of the competition to view */
@path compId: Types.uuid,

...Headers.VersionRequest,
): Responses.OkT<Models.CompetitionBase> | Responses.NotFound;

// ###############################
Expand All @@ -46,6 +48,8 @@ interface Competitions {

/** The application to apply to the competition */
@body application: Models.ApplicationBase,

...Headers.VersionRequest,
):
| Responses.CreatedT<Models.ApplicationBase>
| Responses.NotFound
Expand Down
4 changes: 4 additions & 0 deletions lib/core/lib/core/routes/form-responses.tsp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ interface FormResponses {

/** The response to the form */
@body response: Record<unknown>,

...Headers.VersionRequest,
): Responses.OkT<Models.AppFormResponse>;

// ###############################
Expand All @@ -48,5 +50,7 @@ interface FormResponses {

/** The ID of the form whose response is being retrieved */
@path formId: Types.uuid,

...Headers.VersionRequest,
): Responses.OkT<Models.AppFormResponse>;
}
3 changes: 3 additions & 0 deletions lib/core/lib/core/routes/forms.tsp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ interface Forms {
@get
list(
...Pagination.PaginatedQueryParams,
...Headers.VersionRequest,
): Responses.PaginatedT<Models.FormBase>;

// ###############################
Expand All @@ -36,5 +37,7 @@ interface Forms {
read(
/** The ID of the form to view */
@path formId: Types.uuid,

...Headers.VersionRequest,
): Responses.OkT<Models.FormBase>;
}
5 changes: 5 additions & 0 deletions lib/core/lib/core/routes/opportunities.tsp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ interface Opportunities {
@list
list<T extends Models.OpportunityBase = Models.OpportunityBase>(
...Pagination.PaginatedQueryParams,
...Headers.VersionRequest,
): Responses.PaginatedT<T>;

// ##############################
Expand All @@ -74,6 +75,8 @@ interface Opportunities {
@path
@added(Versions.v0_2)
oppId: Types.uuid,

...Headers.VersionRequest,
): Responses.OkT<T> | Responses.NotFound;

// ###############################
Expand Down Expand Up @@ -106,5 +109,7 @@ interface Opportunities {

/** Pagination instructions for the results */
pagination?: Pagination.PaginatedBodyParams,

...Headers.VersionRequest,
): Responses.FilteredT<T, Models.OppFilters>;
}
11 changes: 11 additions & 0 deletions lib/core/lib/core/routes/orgs.tsp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ interface Organizations {

/** Identifier value within the given registry to filter by */
@query id?: string,

...Headers.VersionRequest,
): Responses.PaginatedT<T> | Responses.Unauthorized | Responses.Forbidden;

// ##############################
Expand All @@ -85,6 +87,8 @@ interface Organizations {

/** Return the profile as of the given point in time */
@query at?: utcDateTime,

...Headers.VersionRequest,
):
| Responses.OkT<T>
| Responses.NotFound
Expand Down Expand Up @@ -112,6 +116,8 @@ interface Organizations {
/** JSON Merge Patch body for the organization profile */
@body
patch: Models.OrgPatchData,

...Headers.VersionRequest,
):
| Responses.OkT<Models.OrgRevision>
| Responses.NotFound
Expand Down Expand Up @@ -141,6 +147,8 @@ interface Organizations {
/** JSON Merge Patch body for the organization profile */
@body
patch: Models.OrgPatchData,

...Headers.VersionRequest,
):
| Responses.AcceptedT<Models.OrgRevision>
| Responses.NotFound
Expand Down Expand Up @@ -168,6 +176,7 @@ interface Organizations {
orgId: Types.uuid,

...Pagination.PaginatedQueryParams,
...Headers.VersionRequest,
):
| Responses.PaginatedT<Models.OrgRevision>
| Responses.NotFound
Expand Down Expand Up @@ -197,6 +206,8 @@ interface Organizations {
/** The id of the change to view */
@path
changeId: Types.uuid,

...Headers.VersionRequest,
):
| Responses.OkT<Models.OrgRevision>
| Responses.NotFound
Expand Down
Loading
Loading