Skip to content
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

Typespec fixes #1608

Merged
merged 10 commits into from
Oct 2, 2024
12 changes: 12 additions & 0 deletions .dagger/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,18 @@ func (m *Generate) WebSdk() *dagger.Directory {
WithoutDirectory("node_modules")
}

func (m *Generate) Server() *dagger.Directory {
openapi := m.Openapi()

source := m.Source.
WithFile("api/openapi.yaml", openapi)

return goModule().
WithSource(source).
Exec([]string{"go", "generate", "-x", "./api"}).
Directory("/work/src/api")
}

func (m *Generate) Check(ctx context.Context) error {
result := goModuleCross("").
WithSource(m.Source).
Expand Down
56 changes: 3 additions & 53 deletions api/spec/src/cloud/subjects.tsp
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ interface Subjects {
*/
@get
@operationId("listSubjects")
list(): Subject[] | OpenMeter.CommonErrors;
list(): OpenMeter.Subject[] | OpenMeter.CommonErrors;

/**
* Get subject by ID or key.
*/
@get
@operationId("getSubject")
get(@path subjectIdOrKey: string): Subject | OpenMeter.NotFoundError | OpenMeter.CommonErrors;
get(@path subjectIdOrKey: string): OpenMeter.Subject | OpenMeter.NotFoundError | OpenMeter.CommonErrors;

/**
* Upserts a subject. Creates or updates subject.
Expand All @@ -33,7 +33,7 @@ interface Subjects {
*/
@post
@operationId("upsertSubject")
upsert(@body subject: Subject[]): Subject[] | OpenMeter.CommonErrors;
upsert(@body subject: OpenMeter.Subject[]): OpenMeter.Subject[] | OpenMeter.CommonErrors;

/**
* Delete subject by ID or key.
Expand All @@ -42,53 +42,3 @@ interface Subjects {
@operationId("deleteSubject")
delete(@path subjectIdOrKey: string): void | OpenMeter.CommonErrors;
}

/**
* A subject is a unique identifier for a user or entity.
*/
@friendlyName("Subject")
@example(#{
id: "01G65Z755AFWAKHE12NY0CQ9FH",
key: "customer-id",
displayName: "Customer Name",
metadata: #{ hubspotId: "123456" },
currentPeriodStart: DateTime.fromISO("2023-01-01T00:00:00Z"),
currentPeriodEnd: DateTime.fromISO("2023-02-01T00:00:00Z"),
stripeCustomerId: "cus_JMOlctsKV8",
})
model Subject {
// Validator doesn't obey required for readOnly properties
// See: https://github.com/stoplightio/spectral/issues/1274

/**
* A unique identifier for the subject.
*/
@visibility("read")
@example("01G65Z755AFWAKHE12NY0CQ9FH")
id: ULID;

/**
* A unique, human-readable identifier for the subject.
* Must consist only alphanumeric and underscore characters.
*/
@example("customer-id")
key: Key;

/**
* A human-readable display name for the subject.
*/
@example("Customer Name")
displayName?: string;

@example(#{ hubspotId: "123456" })
metadata?: Record<string>;

@example(DateTime.fromISO("2023-01-01T00:00:00Z"))
currentPeriodStart?: DateTime;

@example(DateTime.fromISO("2023-02-01T00:00:00Z"))
currentPeriodEnd?: DateTime;

@example("cus_JMOlctsKV8")
stripeCustomerId?: string;
}
8 changes: 5 additions & 3 deletions api/spec/src/entitlements/feature.tsp
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@ interface Features {
/**
* Filter by meterSlug
*/
@query meterSlug?: string,
@query
meterSlug?: string[],

/**
* Filter by meterGroupByFilters
*/
@query includeArchived?: boolean = false,
@query
includeArchived?: boolean = false,

...OpenMeter.QueryPagination,
...OpenMeter.QueryLimitOffset,
Expand Down Expand Up @@ -91,7 +93,7 @@ model FeatureCreateInputs {

@summary("Optional metadata")
@example(#{ key: "value" })
metadata: Metadata;
metadata?: Metadata;

// /**
// * The meter that the feature is associated with and and based on which usage is calculated.
Expand Down
1 change: 1 addition & 0 deletions api/spec/src/main.tsp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import "./customer.tsp";
import "./events.tsp";
import "./meters.tsp";
import "./portal.tsp";
import "./subjects.tsp";
import "./debug.tsp";
import "./notification";
import "./entitlements";
Expand Down
2 changes: 1 addition & 1 deletion api/spec/src/meters.tsp
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ enum WindowSize {
/**
* A unique meter identifier.
*/
alias MeterIdentifier = ULID | Key;
alias MeterIdentifier = ULIDOrKey;

/**
* Meter query parameters.
Expand Down
8 changes: 1 addition & 7 deletions api/spec/src/notification/event.tsp
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ model EventBalanceThresholdPayloadData {

@visibility("read")
@summary("Subject")
subject: Subject;
subject: OpenMeter.Subject;

@visibility("read")
@summary("Entitlement Value")
Expand All @@ -119,12 +119,6 @@ model EventBalanceThresholdPayloadData {
threshold: RuleBalanceThresholdValue;
}

/**
* FIXME: move subject api to OpenMeter so Subject model is avaiable here.
*/
@friendlyName("DummySubject")
model Subject {}

/**
* Type of the notification event.
*/
Expand Down
61 changes: 61 additions & 0 deletions api/spec/src/subjects.tsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import "@typespec/http";
import "@typespec/rest";
import "@typespec/openapi3";

using TypeSpec.Http;
using TypeSpec.Rest;
using TypeSpec.OpenAPI;

namespace OpenMeter;

/**
* A subject is a unique identifier for a user or entity.
*/
@friendlyName("Subject")
@example(#{
id: "01G65Z755AFWAKHE12NY0CQ9FH",
key: "customer-id",
displayName: "Customer Name",
metadata: #{ hubspotId: "123456" },
currentPeriodStart: DateTime.fromISO("2023-01-01T00:00:00Z"),
currentPeriodEnd: DateTime.fromISO("2023-02-01T00:00:00Z"),
stripeCustomerId: "cus_JMOlctsKV8",
})
model Subject {
// Validator doesn't obey required for readOnly properties
// See: https://github.com/stoplightio/spectral/issues/1274

/**
* A unique identifier for the subject.
*/
@visibility("read")
@example("01G65Z755AFWAKHE12NY0CQ9FH")
id: ULID;

/**
* A unique, human-readable identifier for the subject.
* Must consist only alphanumeric and underscore characters.
*/
@example("customer-id")
key: Key;

/**
* A human-readable display name for the subject.
*/
@example("Customer Name")
displayName?: string;

// TODO: figure out if we want to support arbitrary values or string only

@example(#{ hubspotId: "123456" })
metadata?: Record<unknown> | null;

@example(DateTime.fromISO("2023-01-01T00:00:00Z"))
currentPeriodStart?: DateTime;

@example(DateTime.fromISO("2023-02-01T00:00:00Z"))
currentPeriodEnd?: DateTime;

@example("cus_JMOlctsKV8")
stripeCustomerId?: string;
}
17 changes: 17 additions & 0 deletions api/spec/src/types.tsp
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import "@typespec/openapi3";

using TypeSpec.OpenAPI;

/**
* ULID (Universally Unique Lexicographically Sortable Identifier).
*/
Expand All @@ -17,11 +21,24 @@ scalar ULID extends string;
@maxLength(64)
scalar Key extends string;

/**
* ULID (Universally Unique Lexicographically Sortable Identifier).
* A key is a unique string that is used to identify a resource.
*
* TODO: this is a temporary solution to support both ULID and Key in the same spec for codegen.
*/
@pattern("^[a-z0-9]+(?:_[a-z0-9]+)*$|^[0-7][0-9A-HJKMNP-TV-Za-hjkmnp-tv-z]{25}$")
@minLength(1)
@maxLength(64)
scalar ULIDOrKey extends string;

// NOTE (andras): key format enforcement isn't supported by TypeSpec (patternProperties). See: https://github.com/microsoft/typespec/discussions/1626
// TODO: decide if we want to use the generated Metadata type instead and update code to use it
/**
* Set of key-value pairs.
* Metadata can be used to store additional information about a resource.
*/
@extension("x-go-type", "map[string]string")
@example(#{ externalId: "019142cc-a016-796a-8113-1a942fecd26d" })
model Metadata {
...Record<string>;
Expand Down
Loading