namespace vs interface #7419
Replies: 2 comments 4 replies
-
To be more specific, this is the way I'm sometimes encapsulating data models into namespaces, because they make no sense in global context otherwise. In this case I have to have this additional #suppress "@typespec/http/no-service-found" ""
@tag("index")
@route("/organizations/{organizationId}/index/identities")
@useAuth(BearerAuth)
namespace OrganizationIdentitiesIndex {
model Record {
organization: PickProperties<Organization, "id" | "name">;
identity: PickProperties<Identity, "id" | "name" | "avatarUrl">;
authzAttrs: OmitProperties<
MyAPI.Authz.OrganizationIdentityAttributes,
"kind" | "organizationId" | "identityId"
>;
}
@get
op query(
@path organizationId: string,
...QueryPageParameters,
): QueryPage<Record>;
} |
Beta Was this translation helpful? Give feedback.
-
Namespace are just grouping element that will be merged across file with the same namespaces(as your comment said its quite similar to C# and there isn't much more to that). What maybe you are getting confused by is that the openapi3 emitter will do a special treatment of the global namespace when you don't have a service defined. It will count anything directly at the root as part of this service. However if you want to include more then you need to make your own root namespace with @service
@tag("index")
@route("/organizations/{organizationId}/index/identities")
@useAuth(BearerAuth)
namespace OrganizationIdentitiesIndex {
model Record {
organization: PickProperties<Organization, "id" | "name">;
identity: PickProperties<Identity, "id" | "name" | "avatarUrl">;
authzAttrs: OmitProperties<
MyAPI.Authz.OrganizationIdentityAttributes,
"kind" | "organizationId" | "identityId"
>;
}
@get
op query(
@path organizationId: string,
...QueryPageParameters,
): QueryPage<Record>;
} or this way and you can add multiple sub grouping @service
namespace MyService;
@tag("index")
@route("/organizations/{organizationId}/index/identities")
@useAuth(BearerAuth)
namespace OrganizationIdentitiesIndex {
model Record {
organization: PickProperties<Organization, "id" | "name">;
identity: PickProperties<Identity, "id" | "name" | "avatarUrl">;
authzAttrs: OmitProperties<
MyAPI.Authz.OrganizationIdentityAttributes,
"kind" | "organizationId" | "identityId"
>;
}
@get
op query(
@path organizationId: string,
...QueryPageParameters,
): QueryPage<Record>;
} |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I think I'm getting confused over and over with namespaces vs interface for grouping operations. From the documentation you can use both, but in contrast with TS, there's no declaration merging, and e.g. using namespaces to declare inner models gives a default warning of
No namespace with '@service' was found, but Namespace 'OrganizationIdentitiesIndex' contains routes. Did you mean to annotate this with '@service'
On the other hand, interface can't have inner models definitions, so it's not an equivalent option.
Additionally, implementing custom emitter becoming trickier because both namespace and interfaces can be used for "service" definitions, which raises a basic conceptual question: which one to use for which cases? Or what's an intention for both of them.
I read documentation on namespace and interfaces in typespec, I'm fluent with TypeScript and things, but conceptually feels like either namespaces or interfaces aligned poorly in their purposes.
Can somebody please explain the intended use cases for both, besides "namespaces as in C# namespace for grouping and interfaces are classical interfaces", because even this is not completely true for TypeSpec. Like practically, what's to use when.
Beta Was this translation helpful? Give feedback.
All reactions